【问题标题】:Using dplyr summarize with different operations for multiple columns使用 dplyr 对多列进行不同操作的汇总
【发布时间】:2018-08-03 07:18:10
【问题描述】:

嗯,我知道已经有很多相关的问题,但没有一个能回答我的特殊需求。

我想在一个有 50 列的表上使用 dplyr "summarize",我需要对这些应用不同的汇总函数。

“Summarize_all”和“summarize_at”似乎都有一个缺点,即无法将不同的函数应用于不同的变量子组。

例如,假设 iris 数据集有 50 列,因此我们不想按名称来寻址列。我想要前两列的总和,第三列的平均值和所有剩余列的第一个值(在 group_by(Species) 之后)。我怎么能这样做?

【问题讨论】:

  • 不确定我是否正确,但直接引用 here 之类的列号或提取列名并使用这些可能吗?
  • 欢迎来到 Stack Overflow,为了在这里寻求帮助,请考虑how to write a reproducible example,谢谢。
  • 用iris例子给出了可重现的例子。
  • 人们只是逐字重复指南是怎么回事。这个问题很清楚。

标签: r dplyr


【解决方案1】:

幸运的是,现在有一种更简单的方法可用。 随着新的dplyr 1.0.0 即将推出,您可以利用across 函数实现此目的。

您只需要输入:

iris %>% 
  group_by(Species) %>% 
  summarize(
    # I want the sum over the first two columns, 
    across(c(1,2), sum),
    #  the mean over the third 
    across(3, mean),
    # the first value for all remaining columns (after a group_by(Species))
    across(-c(1:3), first)
  )

很好,不是吗? 我最初认为 cross 不是必需的,因为作用域变体工作得很好,但这个用例正是 across 函数非常有益的原因。

您可以通过devtools::install_github("tidyverse/dplyr")获取最新版本的dplyr

【讨论】:

    【解决方案2】:

    this - 功能即将推出

    【讨论】:

      【解决方案3】:

      正如其他人所提到的,这通常通过为要应用汇总函数的每组列调用 summarize_each / summarize_at / summarize_if 来完成。据我所知,您必须创建一个自定义函数来对每个子集进行汇总。例如,您可以设置列名,以便您可以使用 select helpers(例如 contains())来过滤您想要应用该函数的列。如果没有,那么您可以设置要汇总的特定列号。

      对于您提到的示例,您可以尝试以下操作:

      summarizer <- function(tb, colsone, colstwo, colsthree, 
                             funsone, funstwo, funsthree, group_name) {
      
      return(bind_cols(
          summarize_all(select(tb, colsone), .funs = funsone),
          summarize_all(select(tb, colstwo), .funs = funstwo) %>% 
            ungroup() %>% select(-matches(group_name)),
          summarize_all(select(tb, colsthree), .funs = funsthree) %>% 
            ungroup() %>% select(-matches(group_name)) 
      ))
      
      }
      
      #With colnames
      iris %>% as.tibble() %>% 
        group_by(Species) %>% 
        summarizer(colsone = contains("Sepal"), 
               colstwo = matches("Petal.Length"), 
               colsthree = c(-contains("Sepal"), -matches("Petal.Length")),
               funsone = "sum", 
               funstwo = "mean",
               funsthree = "first",
               group_name = "Species")
      
      #With indexes
      iris %>% as.tibble() %>% 
       group_by(Species) %>% 
       summarizer(colsone = 1:2, 
               colstwo = 3, 
               colsthree = 4,
               funsone = "sum", 
               funstwo = "mean",
               funsthree = "first",
               group_name = "Species")
      

      【讨论】:

      • 太棒了!这对我帮助很大,而且效果很好。谢谢!!
      • 其他注意事项:对于函数的附加参数,您可以将它们添加到函数调用中,例如".funs = funsone, na.rm = T),"
      【解决方案4】:

      试试这个:

      library(plyr)
      library(dplyr)
      
      dataframe <- data.frame(var = c(1,1,1,2,2,2),var2 = c(10,9,8,7,6,5),var3=c(2,3,4,5,6,7),var4=c(5,5,3,2,4,2))
      dataframe
      
      #  var var2 var3 var4
      #1   1   10    2    5
      #2   1    9    3    5
      #3   1    8    4    3
      #4   2    7    5    2
      #5   2    6    6    4
      #6   2    5    7    2
      
      funnames<-c(sum,mean,first)
      colnums<-c(2,3,4)
      ddply(.data = dataframe,.variables = "var",
          function(x,funcs,inds){
              mapply(function(func,ind){
                  func(x[,ind])
              },funcs,inds)
          },funnames,colnums)
      
      #  var V1 V2 V3
      #1   1 27  3  5
      #2   2 18  6  2
      

      【讨论】:

        【解决方案5】:

        您可以使用每个函数单独汇总数据,然后在需要时加入数据。

        虹膜示例如下:

        sums <- iris %>% group_by(Species) %>% summarise_at(1:2, sum)
        means <- iris %>% group_by(Species) %>% summarise_at(3, mean)
        firsts <- iris %>% group_by(Species) %>% summarise_at(4, first)
        full_join(sums, means) %>% full_join(firsts)
        

        不过,如果您需要使用的汇总函数不只少数,我会尝试考虑其他方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-06-16
          • 1970-01-01
          • 2016-06-03
          • 2014-12-02
          • 1970-01-01
          • 2022-07-21
          • 2021-03-12
          相关资源
          最近更新 更多