How to use save() function in R when variable names are stored in a vector? -
i have vector varnames has "name" of variables "character". want save particular variables rdata using save(). how should go that?
i trying following:
> varset [1] "blah1" [2] "blah2" > str(vatset) chr [1:44] "blah1" "blah2" ... > foo <- lapply(varset, function(x) as.name(x))
as expected foo list of symbols. thinking of doing like
eval(unlist(foo), file="filename")
i guess unlist(foo) not working. how should solve issue? can clear concept why unlist(foo) not unlisting list of symbols?
edit: adding artificial example
> x <- c(1,2,3) > y <- data.frame(m=c(1,2), n=c(1,2,3))
i can save x , y.
> save(x, y, file="filename.rda")
but suppose have
> varset <- c("x", "y")
in example varset big set. need use varset save corresponding variables names stored.
you can save data object as:
save(varset, file="varset.rdata")
but inquiry sounds bit confused. want save it, or save in particular way, data.frame?
assuming list of lists called varset:
you can use plyr solution:
library (plyr) df <- ldply(varset, data.frame)
or more manually strategy. assuming list has 100 elements:
df <- data.frame(matrix(unlist(varset), nrow=100, byrow=t))
the above convert character columns factors, avoid can add parameter data.frame() call:
df <- data.frame(matrix(unlist(varset), nrow=100, byrow=t),stringsasfactors=false)
Comments
Post a Comment