Let's say I have the following string: str <- "P = 0.0, T = 0.0, Q = 0.0" I'd like to find a function that generates the following object from 'str'. list(P = 0.0, T = 0.0, Q = 0.0) Thanks! -- http://mutualism.williams.edu
Is this what your want? as.vector(unlist(strsplit(str,",")),mode="list") Ross Darnell -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Manuel Morales Sent: Friday, 27 July 2007 10:39 AM To: r-help Subject: [R] Convert string to list? Let's say I have the following string: str <- "P = 0.0, T = 0.0, Q = 0.0" I'd like to find a function that generates the following object from 'str'. list(P = 0.0, T = 0.0, Q = 0.0) Thanks! -- http://mutualism.williams.edu ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.
Is this what you want:> str <- "P = 0.0, T = 0.0, Q = 0.0" > x <- eval(parse(text=paste('list(', str, ')'))) > str(x)List of 3 $ P: num 0 $ T: num 0 $ Q: num 0>On 7/26/07, Manuel Morales <Manuel.A.Morales at williams.edu> wrote:> Let's say I have the following string: > > str <- "P = 0.0, T = 0.0, Q = 0.0" > > I'd like to find a function that generates the following object from > 'str'. > > list(P = 0.0, T = 0.0, Q = 0.0) > > Thanks! > > -- > http://mutualism.williams.edu > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Try this. It pastes "list(" onto the front and ")" onto the end giving "list( P = 0.0, T = 0.0, Q = 0.0 )" and then parses and evaluates that as an R expression. Str <- "P = 0.0, T = 0.0, Q = 0.0" eval(parse(text = paste("list(", Str, ")"))) On 7/26/07, Manuel Morales <Manuel.A.Morales at williams.edu> wrote:> Let's say I have the following string: > > str <- "P = 0.0, T = 0.0, Q = 0.0" > > I'd like to find a function that generates the following object from > 'str'. > > list(P = 0.0, T = 0.0, Q = 0.0) > > Thanks! > > -- > http://mutualism.williams.edu > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >