Re: [R] simplify this instruction
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 given the example, the solution would rather be if (B %in% as.character(0:9)) A else B or maybe if (as.numeric(B) %in% 0:9) A else B Er, ... The solution offered by Velez duplicates the action of the nested ifelse expressions offered by the original poster. Your alternatives do not. The oops, right. should have checked agains B as a non-singleton vector. vQ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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. Of course this is sheer pedantry. thanks, you're so reliable. I'm sure the original poster was really concerned with a numeric variable and used the quote marks around the digits out of confusion over what was required. (Some misguided subconscious notion to the effect that to get character output you need character input.) in any case, the version you propose should do the job. vQ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] simplify this instruction
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: ifelse(B==(0:9),A,B) Best regards -- View this message in context: http://www.nabble.com/simplify-this-instruction-tp20589904p20589904.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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 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: ifelse(B==(0:9),A,B) Best regards -- View this message in context: http://www.nabble.com/simplify-this-instruction-tp20589904p20589904.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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 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 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: ifelse(B==(0:9),A,B) Best regards -- View this message in context: http://www.nabble.com/simplify-this-instruction-tp20589904p20589904.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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 Cs and Ds...). or, of course you can check for B being between 0 and 9 rather than being the respective integers: ifelse ((B 0) (B = 9)), A, B) HTH Claudia 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: ifelse(B==(0:9),A,B) Best regards -- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Università degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste phone: +39 (0 40) 5 58-34 47 email: [EMAIL PROTECTED] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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 or maybe if (as.numeric(B) %in% 0:9) A else B On Wed, Nov 19, 2008 at 4:42 PM, CE.KA [EMAIL PROTECTED] wrote: 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: ifelse(B==(0:9),A,B) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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.character(0:9)) A else B or maybe if (as.numeric(B) %in% 0:9) A else B Er, ... The solution offered by Velez duplicates the action of the nested ifelse expressions offered by the original poster. Your alternatives do not. The ifelse(cond, consequenceA, consequenceB) function operates on a vector and returns a vector of equal length, whereas: if cond consequenceA else consequenceB# only returns a single item. B - 0:12 ifelse(B %in% c(0:9), 'A', 'B') [1] A A A A A A A A A A B B B if (B %in% as.character(0:9)) A else B [1] A Warning message: In if (B %in% as.character(0:9)) A else B : the condition has length 1 and only the first element will be used if (as.numeric(B) %in% 0:9) A else B [1] A Warning message: In if (as.numeric(B) %in% 0:9) A else B : the condition has length 1 and only the first element will be used 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)) [1] A A A A A A A A A A B B B -- David Winsemius Heritage Labs On Wed, Nov 19, 2008 at 4:42 PM, CE.KA [EMAIL PROTECTED] wrote: 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: ifelse(B==(0:9),A,B) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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 sure the original poster was really concerned with a numeric variable and used the quote marks around the digits out of confusion over what was required. (Some misguided subconscious notion to the effect that to get character output you need character input.) cheers, Rolf Turner On 20/11/2008, at 5:06 PM, 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 given the example, the solution would rather be if (B %in% as.character(0:9)) A else B or maybe if (as.numeric(B) %in% 0:9) A else B Er, ... The solution offered by Velez duplicates the action of the nested ifelse expressions offered by the original poster. Your alternatives do not. The ifelse(cond, consequenceA, consequenceB) function operates on a vector and returns a vector of equal length, whereas: if cond consequenceA else consequenceB# only returns a single item. B - 0:12 ifelse(B %in% c(0:9), 'A', 'B') [1] A A A A A A A A A A B B B if (B %in% as.character(0:9)) A else B [1] A Warning message: In if (B %in% as.character(0:9)) A else B : the condition has length 1 and only the first element will be used if (as.numeric(B) %in% 0:9) A else B [1] A Warning message: In if (as.numeric(B) %in% 0:9) A else B : the condition has length 1 and only the first element will be used 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)) [1] A A A A A A A A A A B B B -- David Winsemius Heritage Labs On Wed, Nov 19, 2008 at 4:42 PM, CE.KA [EMAIL PROTECTED] wrote: 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: ifelse(B==(0:9),A,B) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] simplify this instruction
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( argument, c(0,9+1) ) ] which generalizes nicely to many intervals, including large ones. The vector c(A,B) gives the labels; c(0,9+1) specifies the intervals -- it is 9+1 because the intervals here are closed on the left and open on the right. The NA and the 1+... are necessary because otherwise findInterval returns 0 for arguments less than the first interval start, which causes subscripting to drop that element. -s PS I am new to R, but solving questions like this is my way of learning it better. Comments by experienced R users are most welcome! Is there a way to simplify this instruction: ifelse(B==0,A, ifelse(B==1,A, ... ifelse(B==9,A,B)) i am looking for something like this: ifelse(B==(0:9),A,B) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.