我想准确应用此question中的内容,但在我的情况下,我有一个列表而不是一个向量。
我的代码如下:
X_ess <- list(c('a','b','c'),c('d','e','f'))
x <- seq_along(X_ess)
xi <- unlist(lapply(x, function(n) combn(x, n, simplify=FALSE)), recursive=FALSE)
lapply(xi, function(i) X_ess[i])
它不会返回预期的内容。它将返回列表中每个元素的所有可能元组:(a,b),(a,c),(b,c),......,(e,f)。
修改的 我的输出应该如下所示:
[1] "a" "b"
[2] "a" "c"
[3] "b" "c"
[4] "d" "e"
[5] "d" "f"
[6] "e" "f"
答案 0 :(得分:1)
您可以尝试:
library(gtools)
Reduce(rbind,lapply(X_ess, function(x) combinations(length(x), 2, x)))
[,1] [,2]
[1,] "a" "b"
[2,] "a" "c"
[3,] "b" "c"
[4,] "d" "e"
[5,] "d" "f"
[6,] "e" "f"