in r combine a list of lists into one list -
i cannot find out can list of entry of sublists? here's simple example, list o lists:
listoflists <- list("a"=list(c(1,2,3),c(2,34,2)), "b" = list(c(1,2),c(2,3,2,1)), "c" = list(c("sdf",3,2))) $a $a[[1]] [1] 1 2 3 $a[[2]] [1] 2 34 2 $b $b[[1]] [1] 1 2 $b[[2]] [1] 2 3 2 1 $c $c[[1]] [1] "sdf" "3" "2"
i found sad way using for-loop:
listofvectors <- list() (i in 1:length(listoflists)) {listofvectors <- c(listofvectors, listoflists[[i]])}
we can use concatenate function (c
) within do.call
flatten nested list
res <- do.call(c, listoflists) all.equal(listofvectors, res, check.attributes = false) #[1] true
or @d.b mentioned in comments, unlist
recursive = false
can work
unlist(listoflists, recursive = false)
Comments
Post a Comment