r - 列表中所有可能的组合

标签 r dataframe

我的数据框:

data <- structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L,1L,1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), col1 = c(9, 
9.05, 7.15, 7.21, 7.34, 8.12, 7.5, 7.84, 7.8, 7.52, 8.84, 6.98, 
6.1, 6.89, 6.5, 7.5, 7.8, 5.5, 6.61, 7.65, 7.68,8.0,9.0), col2 = c(11L, 
11L, 10L, 1L, 3L, 7L, 11L, 11L, 11L, 11L, 4L, 1L, 1L, 1L, 2L, 
2L, 1L, 4L, 8L, 8L, 1L,3L,4L), col3 = c(7L, 11L, 3L, 7L, 11L, 2L, 11L, 
5L, 11L, 11L, 5L, 11L, 11L, 2L, 9L, 9L, 3L, 8L, 11L, 11L, 2L,5L,6L), 
    col4 = c(11L, 11L, 11L, 11L, 6L, 11L, 11L, 11L, 10L, 7L, 
    11L, 2L, 11L, 3L, 11L, 11L, 6L, 11L, 1L, 11L, 11L,13L,12L), col5 = c(11L, 
    1L, 2L, 2L, 11L, 11L, 1L, 10L, 2L, 11L, 1L, 3L, 11L, 11L, 
    8L, 8L, 11L, 11L, 11L, 2L, 9L,4L,5L)), .Names = c("group", "col1", 
"col2", "col3", "col4", "col5"), class = "data.frame", row.names = c(NA, 
-21L))

功能:

comb <- list(c(2, 4), c(3, 5), c(4, 6))

test.fun <- function(dat) { 
  do.call(rbind, lapply(comb, function(x) {
    SUM <- dat[[x[1]]]+dat[[x[2]]]
    data.frame(NAME = sprintf('Group %s by Group %s', x[1], x[2]),
                SUM)
   
  }))
}

result <- purrr::map_df(split(data, data$group), test.fun, .id = 'Group')

现在,此函数处理 list(c(2, 4), c(3, 5), c(4, 6)) 中的 2 列列表。我希望它能够处理任何金额,例如:

list(c(2, 4, 6), c(3, 5, 6), c(3, 4, 6), c(2, 3), c(3, 5))

最佳答案

这可能有帮助

test.fun <- function(dat, comb) { 
  do.call(rbind, lapply(comb, function(x) {
    SUM <- rowSums(dat[x], na.rm = TRUE)
    data.frame(NAME = paste0("Group ", toString(x)),
                SUM)
   
  }))
}

-测试

comb2 <- list(c(2, 4, 6), c(3, 5, 6), c(3, 4, 6), c(2, 3), c(3, 5))

purrr::map_df(split(data, data$group), test.fun, comb = comb2, .id = 'Group') %>%
    as_tibble

-输出

# A tibble: 115 × 3
   Group NAME            SUM
   <chr> <chr>         <dbl>
 1 1     Group 2, 4, 6  27  
 2 1     Group 2, 4, 6  21.0
 3 1     Group 2, 4, 6  12.2
 4 1     Group 2, 4, 6  16.2
 5 1     Group 2, 4, 6  29.3
 6 1     Group 2, 4, 6  21.1
 7 1     Group 2, 4, 6  19.5
 8 1     Group 2, 4, 6  22.8
 9 1     Group 3, 5, 6  33  
10 1     Group 3, 5, 6  23  
# … with 105 more rows

对于成对的,使用combn

 bind_rows(lapply(comb2, function(x) {
   SUM <- combn(x, 2, FUN = function(y) rowSums(data[y], 
          na.rm = TRUE))
   nm1 <- rep(combn(x, 2, FUN = paste, collapse="_"), 
       each = nrow(data))
   data.frame(NAME= nm1, SUM)}))

关于r - 列表中所有可能的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69624850/

相关文章:

python - 根据没有名称值的列过滤数据框

python - Pandas :按满足条件的列分组

python - pandas 的分类变量

python - 如何合并pandas中的两个数据框?

r - 函数工厂中的力评估

linux - Linux 中的 Rgnuplot : plot window does not appear

r - 如何使用 ggplot2 在单个单元格中绘制具有多个类别的热图?

r - 在 R 中使用 XGBoost 预测类变量

r - 在 r 中使用 grep 和非常大的正则表达式向量

python - 将数据帧的所有行转换为数组并传递给函数