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
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
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,
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 <
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
5 matches
Mail list logo