【发布时间】:2021-08-04 07:36:44
【问题描述】:
我正在尝试查找两个列表的所有组合,但是第二个列表本质上是第一个列表变量的重复,并添加了括号等,如下所示。
other_cols <- c("C", "D", "E", "F")
other_colsRnd <- c("(1|C)", "(1|D)", "(1|E)", "(1|F)")
# I have some code to do combinations from one list:
combos = do.call(c, lapply(seq_along(other_cols), function(y) {
arrangements::combinations(other_cols, y, layout = "l")
}))
theBigList = sapply(combos, paste, collapse = " + ")
> theBigList
[1] "C" "D" "E" "F" "C + D" "C + E" "C + F" "D + E" "D + F"
[10] "E + F" "C + D + E" "C + D + F" "C + E + F" "D + E + F" "C + D + E + F"
我想要theBigList 中的完整组合列表,没有任何重复 C 和 (1|C)
########
编辑
C 或 D 等是“真实”变量的简写版本,看起来更像:
other_cols <- c("Charlie", "Delta", "Echo", "Foxtrot")
other_colsRnd <- c("(1|Charlie)", "(1|Delta)", "(1|Echo)", "(1|Foxtrot)")
########
预期的结果是这样的,尽管存储的顺序并不重要。
theBigList
"C" "(1|C)" "D" "(1|D)" "E" "(1|E)" "F" "(1|F)" "C + D"
"C + (1|D)" "C + E" "C + (1|E)" "C + F" "C + (1|F)"
"D + E" "D + (1|E)" "D + F" "D + (1|F)"
"E + F" "E + (1|F)"
"C + D + E" "(1|C) + D + E" "(1|C) + (1|D) + E" "(1|C) + (1|D) + (1|E)" etc.
有没有办法将lapply 放在lapply 中?
或者,我目前认为我可以comboRnd 例如
combosRnd = do.call(c, lapply(seq_along(other_cols), function(y) {
arrangements::combinations(other_colsRnd, y, layout = "l")
}))
然后从here 中获取灵感,使用var_comb <- expand.grid(combos, combosRnd) 以及某种if 和grep 来检测“相同”的变量,我还没有解决。
编辑
我想我认为,我可以添加组合,例如像
theBigList = sapply(combos, paste, collapse = " + ")
theBigListRnd = sapply(combosRnd, paste, collapse = " + ")
comboBigList = c(theBigList, theBigListRnd)
var_comb <- expand.grid(combos, combosRnd)
var_comb2 <- expand.grid(theBigList, theBigListRnd)
...所以comboBigList 包含所有没有交叉的地方,然后我可以删除任何一个或var_comb 或var_comb2 中与@ 中匹配的任何内容匹配的任何“行” 987654342@列。
是的,这是我之前提出的问题 here 中更简单的部分,但是我已经将其细化到完成这个地狱分析的必要性,因为我似乎可能已经咬得比我能咀嚼的还多。作为补充(希望如此),我会蛮力使用我需要的嵌套。
【问题讨论】:
标签: r list combinations