使用 dplyr 对多个变量的所有可能组合进行分组

Grouping Over All Possible Combinations of Several Variables With dplyr

给定如下情况

library(dplyr)
myData <- tbl_df(data.frame( var1 = rnorm(100), 
                             var2 = letters[1:3] %>%
                                    sample(100, replace = TRUE) %>%
                                    factor(), 
                             var3 = LETTERS[1:3] %>%
                                    sample(100, replace = TRUE) %>%
                                    factor(), 
                             var4 = month.abb[1:3] %>%
                                    sample(100, replace = TRUE) %>%
                                    factor()))

我想对“myData”进行分组,以最终找到按 var2、var3 和 var4 的所有可能组合分组的摘要数据。

我可以创建一个列表,其中包含所有可能的变量组合作为字符值

groupNames <- names(myData)[2:4]

myGroups <- Map(combn, 
              list(groupNames), 
              seq_along(groupNames),
              simplify = FALSE) %>%
              unlist(recursive = FALSE)

我的计划是用 for() 循环为每个变量组合制作单独的数据集,比如

### This Does Not Work
for (i in 1:length(myGroups)){
     assign( myGroups[i]%>%
             unlist() %>%
             paste0(collapse = "")%>%
             paste0("Data"), 
               myData %>% 
               group_by_(lapply(myGroups[[i]], as.symbol)) %>%
               summarise( n = length(var1), 
                             avgVar2 = var2 %>%
                                       mean()))
}

诚然,我不太擅长列表,查找这个问题有点困难,因为 dpyr 更新已经改变了分组的工作方式。

如果有比单独的数据集更好的方法,我很想知道。

当我只按单个变量分组时,我得到了一个类似于上面的循环。

非常感谢任何帮助!谢谢!

这看起来很复杂,可能有一种方法可以用 do 来简化或想象它,但它确实有效。使用您的 myDatamyGroups

results = lapply(myGroups, FUN = function(x) {
    do.call(what = group_by_, args = c(list(myData), x)) %>%
        summarise( n = length(var1), 
                   avgVar1 = mean(var1))
    }
)

> results[[1]]
Source: local data frame [3 x 3]

  var2  n     avgVar1
1    a 31  0.38929738
2    b 31 -0.07451717
3    c 38 -0.22522129

> results[[4]]
Source: local data frame [9 x 4]
Groups: var2

  var2 var3  n    avgVar1
1    a    A 11 -0.1159160
2    a    B 11  0.5663312
3    a    C  9  0.7904056
4    b    A  7  0.0856384
5    b    B 13  0.1309756
6    b    C 11 -0.4192895
7    c    A 15 -0.2783099
8    c    B 10 -0.1110877
9    c    C 13 -0.2517602

> results[[7]]
# I won't paste them here, but it has all 27 rows, grouped by var2, var3 and var4.

我将你的 summarise 调用更改为平均值 var1,因为 var2 不是数字。

我根据@Gregor 的回答和随后的评论创建了一个函数:

library(magrittr)
myData <- tbl_df(data.frame( var1 = rnorm(100), 
                         var2 = letters[1:3] %>%
                                sample(100, replace = TRUE) %>%
                                factor(), 
                         var3 = LETTERS[1:3] %>%
                                sample(100, replace = TRUE) %>%
                                factor(), 
                         var4 = month.abb[1:3] %>%
                                sample(100, replace = TRUE) %>%
                                factor()))

函数combSummarise

combSummarise <- function(data, variables=..., summarise=...){


  # Get all different combinations of selected variables (credit to @Michael)
    myGroups <- lapply(seq_along(variables), function(x) {
    combn(c(variables), x, simplify = FALSE)}) %>%
    unlist(recursive = FALSE)

  # Group by selected variables (credit to @konvas)
    df <- eval(parse(text=paste("lapply(myGroups, function(x){
               dplyr::group_by_(data, .dots=x) %>% 
               dplyr::summarize_( \"", paste(summarise, collapse="\",\""),"\")})"))) %>% 
          do.call(plyr::rbind.fill,.)

    groupNames <- c(myGroups[[length(myGroups)]])
    newNames <- names(df)[!(names(df) %in% groupNames)]

    df <- cbind(df[, groupNames], df[, newNames])
    names(df) <- c(groupNames, newNames)
    df

}

combSummarise的来电

combSummarise (myData, var=c("var2", "var3", "var4"), 
               summarise=c("length(var1)", "mean(var1)", "max(var1)"))

combSummarise (myData, var=c("var2", "var4"), 
               summarise=c("length(var1)", "mean(var1)", "max(var1)"))

combSummarise (myData, var=c("var2", "var4"), 
           summarise=c("length(var1)"))

等等

受到 Gregor 和 dimitris_ps 的回答的启发,我编写了一个 dplyr 风格的函数,该函数对组变量的所有组合运行汇总。

summarise_combo <- function(data, ...) {

  groupVars <- group_vars(data) %>% map(as.name)

  groupCombos <-  map( 0:length(groupVars), ~combn(groupVars, ., simplify=FALSE) ) %>%
    unlist(recursive = FALSE)

  results <- groupCombos %>% 
    map(function(x) {data %>% group_by(!!! x) %>% summarise(...)} ) %>%
    bind_rows()

  results %>% select(!!! groupVars, everything())
}

示例

library(tidyverse)
mtcars %>% group_by(cyl, vs) %>% summarise_combo(cyl_n = n(), mean(mpg))

使用unite创建新列是最简单的方法

library(tidyverse)
df = tibble(
  a = c(1,1,2,2,1,1,2,2),
  b = c(3,4,3,4,3,4,3,4),
  val = c(1,2,3,4,5,6,7,8)
)
print(df)#output1
df_2 = unite(df, 'combined_header', a, b, sep='_', remove=FALSE) #remove=F doesn't remove existing columns
print(df_2)#output2

df_2 %>% group_by(combined_header) %>%
  summarize(avg_val=mean(val)) %>% print()#output3
#avg 1_3 = mean(1,5)=3 avg 1_4 = mean(2, 6) = 4

结果

Output:
output1
 a     b   val
  <dbl> <dbl> <dbl>
1     1     3     1
2     1     4     2
3     2     3     3
4     2     4     4
5     1     3     5
6     1     4     6
7     2     3     7
8     2     4     8

output2
  combined_header     a     b   val
  <chr>           <dbl> <dbl> <dbl>
1 1_3                 1     3     1
2 1_4                 1     4     2
3 2_3                 2     3     3
4 2_4                 2     4     4
5 1_3                 1     3     5
6 1_4                 1     4     6
7 2_3                 2     3     7
8 2_4                 2     4     8

output3
combined_header avg_val
  <chr>             <dbl>
1 1_3                   3
2 1_4                   4
3 2_3                   5
4 2_4                   6