r - 使用 dplyr 0.3.02 中的 group_by 对数据帧进行分组后选择列时出错

标签 r dplyr

对 data.frame 进行分组后,我无法选择第二列

d <- data.frame(x = 1:10, y = runif(1))
d[,2] # selects the second column
d <- group_by(d, x)
d[,2] # produces the error: index out of bounds

最佳答案

认为这是 dplyr 中 grouped_df 对象的预期行为 - 逻辑是在数据仍然分组时不能删除分组变量。考虑这个示例,其中我使用 dplyr 的 select 函数从 grouped_df 中提取变量:

require(dplyr)
d <- data.frame(x = 1:10, y = runif(1), z  = rnorm(2))
d <- group_by(d, x)

select(d, y)  
#Source: local data frame [10 x 2]
#Groups: x
#
#    x         y
#1   1 0.5861766
#2   2 0.5861766
#3   3 0.5861766
#4   4 0.5861766
#5   5 0.5861766
#6   6 0.5861766
#7   7 0.5861766
#8   8 0.5861766
#9   9 0.5861766
#10 10 0.5861766

您可以看到结果包含分组变量,即使在 select 调用中未指定该变量。

select(d, z) # would work the same way

即使您明确排除了分组变量“x”,在使用select时仍然会返回它:

select(d, -x)
#Source: local data frame [10 x 3]
#Groups: x
#
#    x         y         z
#1   1 0.2110696 2.4393919
#2   2 0.2110696 0.8400083
#3   3 0.2110696 2.4393919
#4   4 0.2110696 0.8400083
#5   5 0.2110696 2.4393919
#6   6 0.2110696 0.8400083
#7   7 0.2110696 2.4393919
#8   8 0.2110696 0.8400083
#9   9 0.2110696 2.4393919
#10 10 0.2110696 0.8400083

要仅获取“y”列,您需要先取消数据分组:

ungroup(d) %>% select(y)
#Source: local data frame [10 x 1]
#
#           y
#1  0.5861766
#2  0.5861766
#3  0.5861766
#4  0.5861766
#5  0.5861766
#6  0.5861766
#7  0.5861766
#8  0.5861766
#9  0.5861766
#10 0.5861766

请注意,您可以使用包含分组变量的 [ 的任何子集,例如:

d[, 1:2]

或者

d[, c(1,3)]

关于r - 使用 dplyr 0.3.02 中的 group_by 对数据帧进行分组后选择列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26969365/

相关文章:

r - 使用 tidyverse 按组和整体获取摘要

r - 在数据框中生成新列,按组计算重复项

r - 将带破折号的 quosure 转换为字符串?

r - dplyr:在group_by()之后在summary()中使用自定义函数

r - 如何在glmnet中指定日志链接?

r - 为 R 中的列的唯一组合创建索引

r - 创建具有依赖项的 R 包

r - 使用多个变量的条件来替换 R 中的变量

r - 选择具有其他列的特定值的列的不同值

r - 如何在匹配 R 中的其他列时将特定值从一个数据列复制到另一个数据列?