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
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 >Try: x <- list("c(1,2,3,4)", "c(1,3,4,2)") lapply(x, function(x) eval(parse(text = x))) HTH, --sundar
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)", 佲渃(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
"as.numeric"? spencer graves 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 > > ______________________________________________ > R-help at 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-- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA spencer.graves at pdf.com www.pdf.com <http://www.pdf.com> Tel: 408-938-4420 Fax: 408-280-7915