Bryan Keller
2012-Sep-17 04:04 UTC
[R] Using paste to create and evaluate a variable expression
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?
Bryan
[[alternative HTML version deleted]]
David Winsemius
2012-Sep-17 07:39 UTC
[R] Using paste to create and evaluate a variable expression
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
Hi,
Try this:
expr1<-parse(text=paste(paste0("X",1:2),collapse="+"))
?eval(expr1)
#[1] 7 7 7 7 7 7
A.K.
----- Original Message -----
From: Bryan Keller <bsbkeller at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Monday, September 17, 2012 12:04 AM
Subject: [R] Using paste to create and evaluate a variable expression
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?
Bryan
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org 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.
Jean V Adams
2012-Sep-17 12:36 UTC
[R] Using paste to create and evaluate a variable expression
Bryan,
Try this.
char <- paste("X", 1:2, sep="", collapse="+")
eval(parse(text=char))
Jean
Bryan Keller <bsbkeller@gmail.com> wrote on 09/16/2012 11:04:19
PM:>
> 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?
>
> Bryan
[[alternative HTML version deleted]]
peter dalgaard
2012-Sep-17 16:28 UTC
[R] Using paste to create and evaluate a variable expression
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