I try to create a new list that contain each individual matrix column from an old list. For example:
var_names <- c('a', 'b', 'c', 'd')
var_combi <- list()
for(i in 1:length(var_names)) {var_combi[[i]] <- combn(var_names[1:length(var_names)],i)}
c(var_combi)
Then I got a list object like below. Each listed item is a matrix:
[[1]]
[,1] [,2] [,3] [,4]
[1,] "a" "b" "c" "d"
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a" "a" "a" "b" "b" "c"
[2,] "b" "c" "d" "c" "d" "d"
[[3]]
[,1] [,2] [,3] [,4]
[1,] "a" "a" "a" "b"
[2,] "b" "b" "c" "c"
[3,] "c" "d" "d" "d"
[[4]]
[,1]
[1,] "a"
[2,] "b"
[3,] "c"
[4,] "d"
I want to create a new list new_list
such as below, e.g.
[[1]]
[1] "a"
[[2]]
[1] "b"
[[3]]
[1] "c"
[[4]]
[1] "d"
[[5]]
[1] "a" "b"
[[6]]
[1] "a" "c"
So that I can loop over them or use lapply
to select those variables from my dataset mydata
using mydata[names(mydata) %in% new_list[[i]]]
.
Now I'm struggle to create new_list
and couldn't find a proper solution for it (I tried unlist
, c
and append
). If I have to use for
loop, how do I loop over a list and listed matrix columns here?