Data with the reproducible example
I have the following list
library(arrangements)
a <- list(
arrangements::combinations(x = 5, k = 5),
arrangements::combinations(x = 5, k = 4),
arrangements::combinations(x = 4, k = 4))
Which looks like this:
[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[[2]]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 5
[3,] 1 2 4 5
[4,] 1 3 4 5
[5,] 2 3 4 5
[[3]]
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
What I need
I would like to use some function over each row of each element of the list a (i.e. [[1]], [[2]], [[3]]) so that the output would look something like the following:
[[1]]
"F1 =~ 1 + 2 + 3 + 4 +5"
[[2]]
"F2 =~ 1 + 2 + 3 + 4"
"F3 =~ 1 + 2 + 3 + 5"
"F4 =~ 1 + 2 + 4 + 5"
"F5 =~ 1 + 3 + 4 + 5"
"F6 =~ 2 + 3 + 4 + 5"
[[3]]
"F7 =~ 2 + 3 + 4 + 5"
What I have managed to do so far and where I am stuck
I have managed to produce the output that I need for individual elements of the list. For example, the below for loop produces the output that I need but only for the second element of list a.
z <- list(
arrangements::combinations(x = 5, k = 4))
qq <- vector(mode = "character", length = 0L)
for(i in 1:5) {
qq[i] <- paste0("F", i+1, sep = " =~ ", paste(z[[1]][i, ], collapse=" + "))
}
qq
[1] "F2 =~ 1 + 2 + 3 + 4" "F3 =~ 1 + 2 + 3 + 5" "F4 =~ 1 + 2 + 4 + 5" "F5 =~ 1 + 3 + 4 + 5"
[5] "F6 =~ 2 + 3 + 4 + 5"
I was not able to write the for loop for the whole list because each element of the list has different dimensions and I get an error that the subscript is out of bounds. Also, I cannot tackle individual elements of list a because it is an example and a list might be much larger, so I need a way to automate it.
I suspect that this can be done by combining lapply and apply but I just cannot figure out how to make it work. For example, here lapply and apply work together but I just cannot figure out how to make them output what I need.
lapply(a, function(x) apply(a[[2]], 1, paste, collapse=""))
Thank you in advance