Message-ID: <D75D9802-A1B0-4DF3-BC2D-8E71BFD3E76D@comcast.net>
Date: 2012-09-17T07:39:58Z
From: David Winsemius
Subject: Using paste to create and evaluate a variable expression
In-Reply-To: <CAH+vLwwf5KZNRWH8Bo_ZrS21DwS0QeL_osO580mryMCoFmHkwA@mail.gmail.com>
On Sep 16, 2012, at 9:04 PM, Bryan Keller wrote:
> Is it possible to use "paste" to write out an expression and evaluate it?
Of course.
?as.expression
?parse
?eval
> 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 = "+"))
Instead of noquote, you might want to look at bquote.
>
> Is there a way to tell R that I want to evaluate the text, not just print
> it out as a character?
Evaluate in what context? It looks as though you are building a formula.
parse(text= paste(paste("X", 1, sep = ""), paste("X", 2, sep = ""), sep = "+") )
expression(X1+X2)
--
David Winsemius, MD
Alameda, CA, USA