0

I'm trying to write a function that groups a data frame by variable columns like this:

func <- function(df = df, col.name){
    df%>%
      group_by(col.name)%>%
      summarise(count = n())
}

When I enter the column name as a string (ie: func(df, 'column_A')) I get the following error:

Error: unknown variable to group by : col.name

But when I enter it as not a string I get this error:

Error in filter_impl(.data, dots) : object 'column_A' not found

Is there a good way to do this so I can just put any column name in there and get value counts for that column? Thanks in advance for the assist.

Josh
  • 1,237
  • 4
  • 15
  • 22

0 Answers0