R dplyr summaryize_each & # 8594; "Error: cannot modify the grouping variable"

I am trying to use dplyr to group and sum a DataFrame but keep getting the following error:

Error: Unable to change grouping variable

Here is the code that generates it:

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

      

Here's the structure of the dataframe 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 link I found for this error is another post that suggests ungrouping first to make sure the data is not already grouped. I have tried this with no success.

Thank,

Luke

+3


source to share


2 answers


Don't put quotes around the grouping variable name:



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

      

+6


source


It looks like there were two problems:

  • Grouping variable names were in quotation marks ("Activity" instead of Activity) - Thank you Richard!
  • Without specifying the columns to sum, dplyr tried to sum the average for each column, including the first two columns that contained the grouped variables.


I corrected the code by specifying all columns except grouping as follows:

data_summary <- labeled_dataset %>%
    group_by(Activity) %>%
    summarise_each(funs(mean), tBodyAccmeanX:tGravityAccmeanX)

      

+1


source







All Articles