如何在dplyr中对列索引进行分组

时间:2017-09-27 04:57:32

标签: r

我有一个场景,我在变量中得到列索引,我必须分组并按该变量汇总

 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?

中做到这一点

2 个答案:

答案 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?

获得了帮助