[R] Using paste to create and evaluate a variable expression

peter dalgaard pdalgd at gmail.com
Mon Sep 17 18:28:14 CEST 2012


On Sep 17, 2012, at 06:04 , Bryan Keller wrote:

> Is it possible to use "paste" to write out an expression and evaluate it?
> Suppose I want to add two vectors X1 and X2, defined as follows:
> 
> X1 <- 1:6
> X2 <- 6:1
> 
> If I write the following it looks like what I want but is a character:
> noquote(paste(paste("X", 1, sep = ""), paste("X", 2, sep = ""), sep = "+"))
> 
> Is there a way to tell R that I want to evaluate the text, not just print
> it out as a character?


You need to parse() it first, then eval()uate. (Read the respective help pages for details.)


However:

> fortune("parse")

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
      R-help (February 2005)



-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com




More information about the R-help mailing list