[R] How to fix my nested conditional IF ELSE code?
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jun 15 02:51:49 CEST 2009
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.
>
More information about the R-help
mailing list