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")
>
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("
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"
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 <
?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
>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
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"
7 matches
Mail list logo