我有一个场景,我在变量中得到列索引,我必须分组并按该变量汇总
col_index <- which(sapply(dataframe, function(x) any(x == "Area of Maintenance")))
> col_index
X__7
8
现在我想按col_index
分组,如下面的
df%>%
group_by(df[col_index]) %>%
summarise(count = n()) %>%
as.data.frame()
它给了我以下错误。
Error in mutate_impl(.data, dots) :
Evaluation error: Column index must be at most 1 if positive, not 8.
col_index
具有动态值。我怎么能在r?
答案 0 :(得分:2)
您可以使用group_by_if
df %>%
group_by_if(function(x) any(x == "Area of Maintenance")) %>%
summarise(count = n()) %>%
as.data.frame()
答案 1 :(得分:2)
尝试以下方法:
col_index <- which(sapply(colnames(dataframe), function(x) any(x == "Area of Maintenance")))
df%>%
group_by(.[[col_index]]) %>%
summarise(count = n()) %>%
as.data.frame()
注意:我必须在colnames
中使用sapply
才能让它在我的机器上正常工作
信用:我从dplyr: how to reference columns by column index rather than column name using mutate?
获得了帮助