R create a list of character vectors

Solution 1:

You can try:

apply(adjFinal, 1, function(x) colnames(adjFinal)[which(x==1)])
#$b
#[1] "a" "b" "c"
#$d
#[1] "d"
#$e
#[1] "e" "f"

Consider that you have a sparse matrix and so the above might be inefficient if you have a large matrix, because apply will coerce the sparse matrix to a regular one. To avoid this, you can also try:

adj2<-as(adjFinal, "dgTMatrix")
split(colnames(adjFinal)[adj2@j+1],adj2@i)

which will get the same result, but more efficiently.