Re: [R] conversion

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Wed 29 Jun 2005 - 07:36:58 EST

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



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 29 07:58:05 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:06 EST