Hi, I've been struggling most of the morning with an IF ELSE problem, and I wonder if someone might be able to sort me out. Here's what I need to do (dummy example, my data are more complicated): If type = A or B or C and status = a then count = 1 and status = b then count = 2 and status = c then count = 3 Else if type = D or E or F and status = a then count = 9 and status = b then count = 8 and status = c then count = 7 End Seems simple when I write it like that, but the R code is escaping me. Thanks! Mark Na [[alternative HTML version deleted]]
On 6/14/2009 6:18 PM, Mark Na wrote:> Hi, > I've been struggling most of the morning with an IF ELSE problem, and I > wonder if someone might be able to sort me out. > > Here's what I need to do (dummy example, my data are more complicated): > > If type = A or B or C > and status = a then count = 1 > and status = b then count = 2 > and status = c then count = 3 > > Else if type = D or E or F > and status = a then count = 9 > and status = b then count = 8 > and status = c then count = 7 > > End > > Seems simple when I write it like that, but the R code is escaping me.mydf <- data.frame(type = sample(LETTERS[1:6], 40, replace=TRUE), status = sample(letters[1:3], 40, replace=TRUE)) mydf$count <- with(mydf, ifelse(type %in% c('A','B','C') & status == 'a', 1, ifelse(type %in% c('A','B','C') & status == 'b', 2, ifelse(type %in% c('A','B','C') & status == 'c', 3, ifelse(type %in% c('D','E','F') & status == 'a', 9, ifelse(type %in% c('D','E','F') & status == 'b', 8, ifelse(type %in% c('D','E','F') & status == 'c', 7, NA))))))) mydf type status count 1 C c 3 2 F b 8 3 B b 2 4 A a 1 5 C b 2 6 D c 7 7 C a 1 8 E c 7 9 F a 9 10 E b 8 11 F a 9 12 D a 9 13 A c 3 14 B b 2 15 D b 8 16 C c 3 17 E c 7 18 A c 3 19 B a 1 20 E a 9 21 E b 8 22 E a 9 23 C b 2 24 A b 2 25 F b 8 26 D a 9 27 B c 3 28 E b 8 29 E c 7 30 F b 8 31 B a 1 32 F c 7 33 F b 8 34 E c 7 35 C b 2 36 C a 1 37 F b 8 38 D c 7 39 F a 9 40 D b 8> Thanks! > > Mark Na > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Gabor Grothendieck
2009-Jun-15 00:51 UTC
[R] How to fix my nested conditional IF ELSE code?
Note that TRUE and FALSE become 1 and 0 when used in arithmetic formulae so: result <- with(DF, (type %in% c("A", "B", "C")) * (1 * (status == "a") + 2 * (status == "b") + 3 * (status == "c")) + (type %in% c("D", "E", "F")) * (9 * (status == "a") + 8 * (status == "b") + 7 * (status == "c"))) If none of the conditions hold for row i then result[i] will be 0. On Sun, Jun 14, 2009 at 6:18 PM, Mark Na<mtb954 at gmail.com> wrote:> Hi, > I've been struggling most of the morning with an IF ELSE problem, and I > wonder if someone might be able to sort me out. > > Here's what I need to do (dummy example, my data are more complicated): > > If type = A or B or C > ? ? and status = a then count = 1 > ? ? and status = b then count = 2 > ? ? and status = c then count = 3 > > Else if type = D or E or F > ? ? and status = a then count = 9 > ? ? and status = b then count = 8 > ? ? and status = c then count = 7 > > End > > Seems simple when I write it like that, but the R code is escaping me. > > Thanks! > > Mark Na > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >