2

I'm very new to R. Trying to define a function that groups a data set (group_by) and then creates summary statistics based on the groupings (dplyr, summarise_each).

Without defining a function the following works:

sum_stat <- data %>%
  group_by(column) %>%
  summarise_each(funs(mean), var1:var20)

The following function form does not work:

sum_stat <- function(data, column){
  data %>%
    group_by(column) %>%
    summarise_each(funs(mean), var1:var20)
}

sum_stat(data, column)

The error message returned is:

Error: unknown column 'column'

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
ACMgo
  • 31
  • 4
  • What does `data` look like? – C_Z_ Sep 16 '15 at 18:16
  • dplyr has some hoops when you pass the name of a column in a variable. This seems to be the answer, but is rather ugly: http://stackoverflow.com/q/29678435/1191259 – Frank Sep 16 '15 at 18:23

1 Answers1

3

This is the usual way you'd do this:

foo <- function(data,column){
  data %>%
    group_by_(.dots = column) %>%
    summarise_each(funs(mean))
}

foo(mtcars,"cyl")
foo(mtcars,"gear")
joran
  • 169,992
  • 32
  • 429
  • 468