Extracting vectors from list in R without looping -
this simple question, have googled hours without satisfying answer. let's suppose have list following one:
thelist <- list(c("de", "labore", "solis"), c("sapiento", "post", "eventum"), c("sursum", "corda")) > thelist [[1]] [1] "de" "labore" "solis" [[2]] [1] "sapiento" "post" "eventum" [[3]] [1] "sursum" "corda" if want print vectors compose list think of like
for(i in 1:length(thelist)) { print(thelist[[i]]) } [1] "de" "labore" "solis" [1] "sapiento" "post" "eventum" [1] "sursum" "corda" however there must more elegant solution, using member of apply family...
i think got answer after doing more googling , experimenting...
extract <- function(m) { sapply(seq_along(m), function(x) m[[x]]) } > extract(thelist) [[1]] [1] "de" "labore" "solis" [[2]] [1] "sapiento" "post" "eventum" [[3]] [1] "sursum" "corda" so matter of performing desired operation (e.g. printing) on result sapply
Comments
Post a Comment