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.
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 <ce.kaya75@yahoo.fr> 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]]
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 <= 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: cbeleites at units.it