Re: [R] Neat conditional assignment

2007-12-10 Thread Gabor Grothendieck
On Dec 10, 2007 6:52 PM, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > With this specific example this will work: > > as.numeric(factor(a)) > > If that is not the real case but its something else you could try any > of these which probably generalize better: > > (a == "A") + 2 * (a == "B") >

Re: [R] Neat conditional assignment

2007-12-10 Thread Ted Harding
On 10-Dec-07 23:41:34, Neil Stewart wrote: > I would like to make a new vector with values taken from two > existing vectors conditional upon a third. At present I am > doing this with a for loop but wonder if there is not a neater > way, given some of the very neat things R can do. > > a<-rep(c("

Re: [R] Neat conditional assignment

2007-12-10 Thread Achim Zeileis
On Mon, 10 Dec 2007, Neil Stewart wrote: > I would like to make a new vector with values taken from two existing > vectors conditional upon a third. At present I am doing this with a for loop > but wonder if there is not a neater way, given some of the very neat things > R can do. > > a<-rep(c("A"

Re: [R] Neat conditional assignment

2007-12-10 Thread Gabor Grothendieck
With this specific example this will work: as.numeric(factor(a)) If that is not the real case but its something else you could try any of these which probably generalize better: (a == "A") + 2 * (a == "B") 1 + (a == "B") ifelse(a == "A", 1, 2) On Dec 10, 2007 6:41 PM, Neil Stewart <

Re: [R] Neat conditional assignment

2007-12-10 Thread jim holtman
?ifelse > a<-rep(c("A","B"),50) > b<-rep(1,100) > c<-rep(2,100) > d <- ifelse(a == "A", b, c) > head(d) [1] 1 2 1 2 1 2 On Dec 10, 2007 3:41 PM, Neil Stewart <[EMAIL PROTECTED]> wrote: > I would like to make a new vector with values taken from two existing > vectors conditional upon a third. At

Re: [R] Neat conditional assignment

2007-12-10 Thread markleeds
>From: Neil Stewart <[EMAIL PROTECTED]> >Date: 2007/12/10 Mon PM 05:41:34 CST >To: [EMAIL PROTECTED] >Subject: [R] Neat conditional assignment d <- ifelse(a == "A",b,c) >I would like to make a new vector with values taken from two existing >vectors conditiona

[R] Neat conditional assignment

2007-12-10 Thread Neil Stewart
I would like to make a new vector with values taken from two existing vectors conditional upon a third. At present I am doing this with a for loop but wonder if there is not a neater way, given some of the very neat things R can do. a<-rep(c("A","B"),50) b<-rep(1,100) c<-rep(2,100) a is thus "A"