[R] conversion

Marc Schwartz (via MN) mschwartz at mn.rr.com
Tue Jun 28 23:36:58 CEST 2005


On Tue, 2005-06-28 at 17:23 -0400, Anna Oganyan wrote:
> Dear List,
> How can I convert a list with elements being character strings, like: 
> "c(1,2,3,4)", â€œc(1,3,4,2) â€¦ to a list with elements as numerical 
> vectors: c(1,2,3,4), c(1,3,4,2)â€¦?
> Thanks!
> Anna

> l <- list("c(1,2,3,4)", "c(1,3,4,2)")

> l
[[1]]
[1] "c(1,2,3,4)"

[[2]]
[1] "c(1,3,4,2)"

Now use lapply() over each list element in 'l', converting the character
vectors to R expressions and then evaluating them:

> lapply(l, function(x) eval(parse(text = x)))
[[1]]
[1] 1 2 3 4

[[2]]
[1] 1 3 4 2


See ?lapply, ?eval and ?parse.

HTH,

Marc Schwartz




More information about the R-help mailing list