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

2012-09-17 Thread peter dalgaard
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: > n

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

2012-09-17 Thread arun
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 To: r-help@r-project.org Cc: Sent: Monday, September 17, 2012 12:04 AM Subject: [R] Using paste to create and evaluate a variable expression

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

2012-09-17 Thread Bryan Keller
This is perfect, thanks! On Mon, Sep 17, 2012 at 7:16 AM, arun wrote: > 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 > To: r-help@r-project.org > Cc: > Sent: Monday,

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

2012-09-17 Thread Jean V Adams
Bryan, Try this. char <- paste("X", 1:2, sep="", collapse="+") eval(parse(text=char)) Jean Bryan Keller 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 <

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

2012-09-17 Thread David Winsemius
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 loo