Message-ID: <621034DA-E40B-478B-974E-F17B8B6FE6D4@gmail.com>
Date: 2012-09-17T16:28:14Z
From: Peter Dalgaard
Subject: Using paste to create and evaluate a variable expression
In-Reply-To: <CAH+vLwwf5KZNRWH8Bo_ZrS21DwS0QeL_osO580mryMCoFmHkwA@mail.gmail.com>
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