0

I am trying to write a little top_n_by function in dplyr.

topNby = function(df, dim, byDim, n) {
  return( group_by(df, as.symbol(dim)) %>%
          summarise(topDim = sum(byDim)) %>%
          arrange(desc(topDim)) %>% 
          slice(1:n) %>%
          .[[dim]] %>% as.character() 
          )
}

But group_by throw back an error:

Error in mutate_impl(.data, dots) : object 'yourDim' not found

I also tried without as.symbol but with no success. What is the correct way to use group_by when you want to be able to dynamically set the column(s) of grouping.

(I am pretty sure this must have been answered elsewhere but cannot find a satisfying answer...)

xav
  • 4,101
  • 5
  • 26
  • 32
  • 2
    Did you try with `group_by_` (notice the added underscore)? – Rich Scriven Oct 20 '15 at 19:18
  • It works! Thanks a lot! With and without `as.symbol`! Any idea on where to find doc on this? Shall we just always use `group_by_` or do certain scenario require `group_by`. The doc is pretty minimal : http://www.rdocumentation.org/packages/dplyr/functions/group_by – xav Oct 20 '15 at 19:34
  • I think it's in the vignettes somewhere. I'll see if I can find it – Rich Scriven Oct 20 '15 at 19:39
  • See this [vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html) for more info. – JasonAizkalns Oct 20 '15 at 19:48
  • Thanks! I actually found that I also needed `summarise_` ! – xav Oct 20 '15 at 20:03
  • By the way, the doc `?summarise_` refers you to the vignette in describing its `.dots` argument. – Frank Oct 20 '15 at 20:11

0 Answers0