0

I want to apply exactly what's done in this question but in my case I have a list instead of one vector.

My code is the following:

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])

it doesn't return what it is expected. It would return all possible tuples for each element in my list: (a,b),(a,c),(b,c),......,(e,f).

EDIT My output should look like this:

[1] "a" "b"
[2] "a" "c"
[3] "b" "c"
[4] "d" "e"
[5] "d" "f"
[6] "e" "f"
Community
  • 1
  • 1
Simplytif
  • 51
  • 7

1 Answers1

1

You can try:

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" 
Chris
  • 6,302
  • 1
  • 27
  • 54