R - Reshape list with matrices

69 views Asked by At

I am working with a list of matrices that was created from the output of combn: lapply(2:length(c("A","B","C","D","E")), function(n) combn(c("A","B","C","D","E"), n, simplify = TRUE))

It looks like this:

[[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A"  "A"  "A"  "A"  "B"  "B"  "B"  "C"  "C"  "D"  
[2,] "B"  "C"  "D"  "E"  "C"  "D"  "E"  "D"  "E"  "E"  

[[2]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A"  "A"  "A"  "A"  "A"  "A"  "B"  "B"  "B"  "C"  
[2,] "B"  "B"  "B"  "C"  "C"  "D"  "C"  "C"  "D"  "D"  
[3,] "C"  "D"  "E"  "D"  "E"  "E"  "D"  "E"  "E"  "E"  

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,] "A"  "A"  "A"  "A"  "B" 
[2,] "B"  "B"  "B"  "C"  "C" 
[3,] "C"  "C"  "D"  "D"  "D" 
[4,] "D"  "E"  "E"  "E"  "E" 

[[4]]
     [,1]
[1,] "A" 
[2,] "B" 
[3,] "C" 
[4,] "D" 
[5,] "E" 

However, I would like to "flatten" this list, reshaping the inner matrices based on their columns.

The result would be a 26-element list that would look like the following:

[[1]]
"A" "B"

[[2]]
"A" "C"

[[3]]
"A" "D"

[[4]]
"A" "E"

[[5]]
"B" "C"

[[6]]
"B" "D"

[[7]]
"B" "E"
.
.
.

[[21]]
"A" "B" "C" "D"

[[22]]
"A" "B" "C" "E"

[[23]]
"A" "B" "D" "E"

[[24]]
"A" "C" "D" "E"

[[25]]
"B" "C" "D" "E"

[[26]]
"A" "B" "C" "D" "E"

How can I achieve that?

2

There are 2 answers

0
Onyambu On BEST ANSWER
unlist(Map(combn, list(LETTERS[1:5]), 2:5, simplify = FALSE), FALSE)

unlist(lapply(2:5, combn, x = LETTERS[1:5], simplify = FALSE), FALSE)

If you already have the results as shown in the question, you could use asplit:

res <- lapply(2:5, combn, x = LETTERS[1:5]) # as shown in the question
do.call(c, lapply(res, asplit, 2))

[[1]]
[1] "A" "B"

[[2]]
[1] "A" "C"

[[3]]
[1] "A" "D"

[[4]]
[1] "A" "E"

[[5]]
[1] "B" "C"

[[6]]
[1] "B" "D"

[[7]]
[1] "B" "E"

[[8]]
[1] "C" "D"

[[9]]
[1] "C" "E"

[[10]]
[1] "D" "E"

[[11]]
[1] "A" "B" "C"

[[12]]
[1] "A" "B" "D"

[[13]]
[1] "A" "B" "E"

[[14]]
[1] "A" "C" "D"

[[15]]
[1] "A" "C" "E"

[[16]]
[1] "A" "D" "E"

[[17]]
[1] "B" "C" "D"

[[18]]
[1] "B" "C" "E"

[[19]]
[1] "B" "D" "E"

[[20]]
[1] "C" "D" "E"

[[21]]
[1] "A" "B" "C" "D"

[[22]]
[1] "A" "B" "C" "E"

[[23]]
[1] "A" "B" "D" "E"

[[24]]
[1] "A" "C" "D" "E"

[[25]]
[1] "B" "C" "D" "E"

[[26]]
[1] "A" "B" "C" "D" "E"
0
ThomasIsCoding On

You can use powerSet from rje package

> library(rje)

> Filter(\(x) length(x) > 1, powerSet(LETTERS[1:5]))
[[1]]
[1] "A" "B"

[[2]]
[1] "A" "C"

[[3]]
[1] "B" "C"

[[4]]
[1] "A" "B" "C"

[[5]]
[1] "A" "D"

[[6]]
[1] "B" "D"

[[7]]
[1] "A" "B" "D"

[[8]]
[1] "C" "D"

[[9]]
[1] "A" "C" "D"

[[10]]
[1] "B" "C" "D"

[[11]]
[1] "A" "B" "C" "D"

[[12]]
[1] "A" "E"

[[13]]
[1] "B" "E"

[[14]]
[1] "A" "B" "E"

[[15]]
[1] "C" "E"

[[16]]
[1] "A" "C" "E"

[[17]]
[1] "B" "C" "E"

[[18]]
[1] "A" "B" "C" "E"

[[19]]
[1] "D" "E"

[[20]]
[1] "A" "D" "E"

[[21]]
[1] "B" "D" "E"

[[22]]
[1] "A" "B" "D" "E"

[[23]]
[1] "C" "D" "E"

[[24]]
[1] "A" "C" "D" "E"

[[25]]
[1] "B" "C" "D" "E"

[[26]]
[1] "A" "B" "C" "D" "E"