[R] help with nesting if else statements

Rodrigo Ângelo dr|go@@nge|o @end|ng |rom gm@||@com
Wed Sep 23 19:08:48 CEST 2020


Hi Ana,

The ifelse function works like this:

*ifelse(condition, if.true, if.false)*

it will check the condition, and if, and only if, condition is true, it
will execute whatever is in if.true,

and if condition is false (and only if the condition is false) it will
execute what's in if.false.

when nesting it, you should establish an order of priorities of your rules,
and put the one with most priority first.

In your case, it seems that the rule

*PLASER==2 -> PHENO==2*

has greater priority over

*CURRELIG==1 -> PHENO==1*

so the order should be the opposite.

On Wed, Sep 23, 2020, 13:44 Ana Marija <sokovic.anamarija using gmail.com> wrote:

> Hello,
>
> I have a data frame as shown bellow.
> I want to create a new column PHENO which will be defined as follows:
> if CURRELIG==1 -> PHENO==1
> in the above subset those that have:
> PLASER==2 -> PHENO==2
> and
> those where RTNPTHY==1 -> PHENO==1
>
> I tried doing this:
> a$PHENO=ifelse(a$CURRELIG==1 | a$RTNPTHY==1  ,1,ifelse(a$PLASER==2 |
> a$RTNPTHY==2,2,NA))
>
> but this give me some lines where I am not seeing results that I want,
> for example:
> FID           IID CURRELIG PLASER RTNPTHY PHENO
> fam5628 G5628        1                 2       2                1
>
> here the PHENO should be =2 because RTNPTHY==2 and PLASER==2
> PHENO should be ==2 when either RTNPTHY==2 or PLASER==2
>
> another wrong line is this:
> FID              IID CURRELIG PLASER RTNPTHY PHENO
> fam5706    G5706        1            1                 2             1
>
> again RTNPTHY ==2 and PHENO==1 instead of 2.
>
> My data looks like this:
> FID  IID CURRELIG PLASER RTNPTHY
> fam5610 G5610        1      1       1
> fam5614 G5614        1      2       2
> fam5615 G5615        1      1       1
> fam5618 G5618        1      1       2
> fam5621 G5621        1      1       1
> fam5624 G5624        1      1       2
> fam5625 G5625        1      1       1
> fam5628 G5628        1      2       2
> fam5633 G5633        1      2       2
> fam5634 G5634        1      1       1
> fam5635 G5635        2      2       2
> fam5636 G5636        1      1       1
> fam5641 G5641        1      1       1
> fam5645 G5645        2      1       2
> fam5646 G5646        2      2       2
> fam5654 G5654        1      2       2
> fam5655 G5655        1      2       2
> fam5656 G5656        2      2       2
> fam5658 G5658        1      1       1
> fam5659 G5659        2      2       2
> fam5660 G5660        1      1       1
> fam5661 G5661        2      2       2
> fam5664 G5664        1      1       1
> fam5666 G5666        1      1       1
> fam5667 G5667        1      1       2
> fam5670 G5670        1      1       1
> fam5671 G5671        1      1       2
> fam5672 G5672        1      1       2
> fam5673 G5673        1      1       1
> fam5680 G5680        1      2       2
> fam5686 G5686        1      2       2
> fam5687 G5687        1      2       2
> fam5688 G5688        1      1       2
> fam5693 G5693        2      1       1
> fam5695 G5695        1      1       1
> fam5697 G5697        1      1       1
> fam5700 G5700        1      2       2
> fam5701 G5701        1      1       1
> fam5706 G5706        1      1       2
> fam5709 G5709        1      1       1
> fam5713 G5713        1      1       1
> fam5715 G5715        1      1       1
> fam5718 G5718        1      1       1
>
> Please advise,
> Ana
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list