Re: [R] simplify this instruction

2008-11-20 Thread Wacek Kusnierczyk
Rolf Turner wrote: > > The issue worrying Wacek was presumably the fact that > the original poster was treating the ``B'' variable as > being character, rather than numeric. > > So (presumably) what he *really* wanted to say was > (something like) > > ifelse(B%in%as.character(0:9),"A","B") yepp.

Re: [R] simplify this instruction

2008-11-20 Thread Wacek Kusnierczyk
David Winsemius wrote: > > On Nov 19, 2008, at 8:38 PM, Wacek Kusnierczyk wrote: > >> Jorge Ivan Velez wrote: >>> B=0:12 B >>> [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 >>> ifelse(B%in%c(0:9),'A','B') >>> [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B" >>> >>> >> >

Re: [R] simplify this instruction

2008-11-19 Thread Stavros Macrakis
The previous solutions assume you are only interested in one small interval, but if your original example is just a simplified version, and the real problem is multiple, possibly large, intervals, you might want to try something like this: c(NA,"A","B")[1+findInterval( <>, c(0,9+1) ) ] which

Re: [R] simplify this instruction

2008-11-19 Thread Rolf Turner
The issue worrying Wacek was presumably the fact that the original poster was treating the ``B'' variable as being character, rather than numeric. So (presumably) what he *really* wanted to say was (something like) ifelse(B%in%as.character(0:9),"A","B") Of course this is sheer pedantry. I'm s

Re: [R] simplify this instruction

2008-11-19 Thread David Winsemius
On Nov 19, 2008, at 8:38 PM, Wacek Kusnierczyk wrote: Jorge Ivan Velez wrote: B=0:12 B [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 ifelse(B%in%c(0:9),'A','B') [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B" given the example, the solution would rather be if (B %in% as.chara

Re: [R] simplify this instruction

2008-11-19 Thread Wacek Kusnierczyk
Jorge Ivan Velez wrote: > >> B=0:12 >> B >> > [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 > >> ifelse(B%in%c(0:9),'A','B') >> > [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B" > > given the example, the solution would rather be if (B %in% as.character(0:9)) "A" else "B"

Re: [R] simplify this instruction

2008-11-19 Thread Claudia Beleites
in addition to %in% and depending on what the general principle behind the setup is: you may want to have a look into switch (e.g. if there happen to be "C"s and "D"s...). or, of course you can check for B being between 0 and 9 rather than being the respective integers: ifelse ((B > 0) && (B

Re: [R] simplify this instruction

2008-11-19 Thread Jorge Ivan Velez
I'm sorry, a typo: > B=0:12 > B [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 > ifelse(B%in%c(0:9),'A','B') [1] "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" "B" "B" "B" HTH, Jorge On Wed, Nov 19, 2008 at 5:16 PM, Jorge Ivan Velez <[EMAIL PROTECTED]>wrote: > > Dear CE.KA, > > Try this: > > B=0:12 >

Re: [R] simplify this instruction

2008-11-19 Thread Jorge Ivan Velez
Dear CE.KA, Try this: B=0:12 > B [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 > ifelse(B%in%c(0,9),'A','B') [1] "A" "B" "B" "B" "B" "B" "B" "B" "B" "A" "B" "B" "B" HTH, Jorge On Wed, Nov 19, 2008 at 4:42 PM, CE.KA <[EMAIL PROTECTED]> wrote: > > Hi R users, > > Is there a way to simplify thi

[R] simplify this instruction

2008-11-19 Thread CE.KA
Hi R users, Is there a way to simplify this instruction: ifelse(B=="0","A", ifelse(B=="1","A", ifelse(B=="2","A", ifelse(B=="3","A", ifelse(B=="4","A", ifelse(B=="5","A", ifelse(B=="6","A", ifelse(B=="7","A", ifelse(B=="8","A", ifelse(B=="9","A","B")) i am looking for something like this