【问题标题】:how to group by on column index in dplyr如何在 dplyr 中按列索引分组
【发布时间】:2018-03-08 11:08:11
【问题描述】:

我有一个场景,我在一个变量中获取列索引,我必须按该变量进行分组和汇总

 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 中做到这一点?

【问题讨论】:

  • 试试group_by_at(col_index)

标签: r


【解决方案1】:

您可以使用group_by_if按与给定函数匹配的所有列分组:

df %>%
    group_by_if(function(x) any(x == "Area of Maintenance")) %>%
    summarise(count = n()) %>%
    as.data.frame()

【讨论】:

  • 感谢您的回答,但如果我想使用 col_index 这样做怎么办?我们可以这样做吗?
【解决方案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()

注意:我必须在 sapply 中使用 colnames 才能让它在我的机器上正常工作

学分:我得到了dplyr: how to reference columns by column index rather than column name using mutate?的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2022-11-17
    • 2022-09-27
    • 2017-12-31
    • 1970-01-01
    相关资源
    最近更新 更多