Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R dplyr summarize_each --> "Error: cannot modify grouping variable"

Tags:

r

dplyr

I'm trying to use dplyr to group and summarize a dataframe, but keep getting the following error:

Error: cannot modify grouping variable

Here's the code that generates it:

data_summary <- labeled_dataset %>%
    group_by("Activity") %>%
    summarise_each(funs(mean))

Here's the structure of the data frame that I'm applying this to:

> str(labeled_dataset)
'data.frame':   10299 obs. of  88 variables:
 $ Subject                          : int  1 1 1 1 1 1 1 1 1 1 ...
 $ Activity                         : Factor w/ 6 levels "LAYING","SITTING",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ tBodyAccmeanX                    : num  0.289 0.278 0.28 0.279 0.277 ...
 $ tBodyAccmeanY                    : num  -0.0203 -0.0164 -0.0195 -0.0262 -0.0166 ...
 $ tBodyAccmeanZ                    : num  -0.133 -0.124 -0.113 -0.123 -0.115 ...
 $ tGravityAccmeanX                 : num  0.963 0.967 0.967 0.968 0.968 ...
 $ tGravityAccmeanY                 : num  -0.141 -0.142 -0.142 -0.144 -0.149 ...
 $ tGravityAccmeanZ                 : num  0.1154 0.1094 0.1019 0.0999 0.0945 ...
   ...

The only reference I've found to this error is another post that suggests ungrouping first to make sure the data isn't already grouped. I've tried that without success.

Thanks,

Luke

like image 935
Luke Avatar asked Mar 17 '23 13:03

Luke


1 Answers

Don't put the name of the grouping variable in quotes:

data_summary <- labeled_dataset %>%
  group_by(Activity) %>%
  summarise_each(funs(mean))
like image 71
Sven Hohenstein Avatar answered Mar 20 '23 14:03

Sven Hohenstein