R dplyr summarize_each - > “错误:不能修改分组变量” [英] R dplyr summarize_each --> "Error: cannot modify grouping variable"

查看:159
本文介绍了R dplyr summarize_each - > “错误:不能修改分组变量”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图使用dplyr对数据框进行分组和总结,但是会出现以下错误:错误:无法修改分组变量


这是生成它的代码:

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

以下是我将此应用于的数据框架的结构:

 > str(labeled_dataset)
'data.frame':10299 obs。 88个变量:
$主题:int 1 1 1 1 1 1 1 1 1 1 ...
$活动:因子w / 6级别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 ...
...

我发现这个错误的唯一参考是另一个帖子,建议首先取消分组,以确保数据尚未分组。我没有成功尝试过。



谢谢,



Luke

解决方案

不要将分组变量的名称用引号括起来:

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


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

解决方案

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

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

这篇关于R dplyr summarize_each - &gt; “错误:不能修改分组变量”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆