0

I wanted to define a function in R to find out the mode of a vector. I made a simple one which is shown below:

modeFinder <- function(par) {
  modeTable <- par %>% table() %>% sort(decreasing = TRUE) %>% data.frame()
  modeVal <- modeTable$Var1[1] %>% as.character()
  return(modeVal)
}

However, when I use this function it always returns empty values. I actually wanted to use this for my summarize function but it doesn't work. I tried to diagnose by segregating the steps like below and it works completely fine that way!

# let's assume a vector "Vic" with expected output "m"
Vic <- c(rep("m", 3), rep("g", 2))
temp <- Vic %>% table() %>% sort(decreasing = TRUE) %>% data.frame()
temp2 <- temp$Var1[1] %>% as.character()

I just can't understand why the custom function is not working while the same code is working outside the function.

I would really appreciate any solution to this. Thanks!

EDIT: I think my question is not clear to some. I just want to know what the problem is in my custom function. I am not looking for any inbuilt function or an alternative to this.

d_vikas
  • 3
  • 4
  • Could you give an example vector `Vic` and associated expected output? – Frank Sep 27 '17 at 17:48
  • `Vic <- c(rep("m", 3), rep("g", 2))` Output would be: "m" character – d_vikas Sep 27 '17 at 17:50
  • `Vic %>% table %>% sort(decreasing=TRUE) %>% as.data.frame %>% extract2(".") %>% extract(1) %>% as.character` seems to work, though it's kind of convoluted. This is assuming you have loaded magrittr. If performance matters, you'll want to look at some of these https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode pipes can slow things down. – Frank Sep 27 '17 at 17:52
  • The set of R statements written below works fine. It's the first one, or the function, that isn't working for some reason. That is my question. – d_vikas Sep 27 '17 at 17:54
  • 1
    Try running the code you think works in a fresh R session with just magrittr loaded. It does not work, as far as I can see, since `temp` will have a column named `"."`, not `"Var1"`. – Frank Sep 27 '17 at 18:00
  • 1
    Possible duplicate of [Is there a built-in function for finding the mode?](https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode) – M-- Sep 27 '17 at 18:10
  • it's not a duplicate, he doesn't want an alternative, he want to know why his code doesn't work – moodymudskipper Sep 28 '17 at 12:09
  • 1
    so your code doesn't work either way (in the function or outside), if you replace `$Var1[1]` by `[1,1]` it will run fine – moodymudskipper Sep 28 '17 at 12:14
  • 1
    @Moody_Mudskipper - Thanks a lot. It works now. But it still doesn't answer my question. What was the problem in my function definition? – d_vikas Sep 29 '17 at 17:45
  • For me (and for Moody_Mudskipper) your code doesn't work. It doesn't work in the function, and it doesn't work out of the function. So the problem in your function definition is that is uses code that doesn't work. I think if you start a new R session and run your code outside of the function, you will also find that it doesn't work. – Gregor Thomas Sep 29 '17 at 18:07
  • @Gregor, I think you guys need to use "." instead of "Var1". Or temp$.[1] instead of temp$Var1[1]. The table column name shows as Var1 in my R studio but as a "." for others. – d_vikas Sep 29 '17 at 18:51
  • Well, if I switch to `temp$.[1]` then the code works for me both inside the function and outside the function. I can't reproduce a case where there is a difference inside the function vs outside. – Gregor Thomas Sep 29 '17 at 19:00
  • It is happening in my RStudio. I have also updated to the latest versions of both my R and RStudio. But still, I see "Var1" instead of "." and the function doesn't work while the same code outside works fine. That was my question: what is the reason behind this function not working fine? – d_vikas Sep 30 '17 at 18:25
  • We can't replicate your problem, so we can't tell you what it is. Are you starting in a fresh R session? Are you loading packages that override, say, the default `table` function? You reference RStudio... is the problem specific to working in RStudio or does it also happen if you use the R command line or the default R GUI? – Gregor Thomas Oct 02 '17 at 15:06

0 Answers0