help with nesting if else statements

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

help with nesting if else statements

anikaM
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

anikaM
I tried doing this:
a$PHENO=ifelse(a$PLASER==2 | a$RTNPTHY==2,2,ifelse(a$CURRELIG==1 |
a$RTNPTHY==1,1,NA))

which brought be closer to the solution, but now I have lines like this:
FID           IID CURRELIG PLASER RTNPTHY PHENO
fam3151 G3151        1      1      NA    NA
fam3149 G3149        2      1      NA    NA
fam3151 G3151        1      1      NA    NA
fam0637  G637        2     NA      NA    NA
fam4483 G4483        1     NA      NA    NA

I would like these lines to look like this:

FID           IID CURRELIG PLASER RTNPTHY PHENO
fam3151 G3151        1      1      NA    1
fam3149 G3149        2      1      NA    2
fam3151 G3151        1      1      NA   1
fam0637  G637        2     NA      NA    2
fam4483 G4483        1     NA      NA    1

in addition to what this command does
a$PHENO=ifelse(a$PLASER==2 | a$RTNPTHY==2,2,ifelse(a$CURRELIG==1 |
a$RTNPTHY==1,1,NA))

On Wed, Sep 23, 2020 at 11:43 AM Ana Marija <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

Rodrigo Ângelo
In reply to this post by anikaM
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

Bert Gunter-2
In reply to this post by anikaM
Nested ifelse()'s are confusing and invite error.

Just use ?within and subscript with your conditions:

dat$PHENO <- NA  ## initialize PHENO
> dat <-  ## to return the modified result
within(dat, {
+     PHENO[CURRELIG ==1] <- 1
+     PHENO[CURRELIG == 1 & PLASER == 2] <- 2
+     PHENO[CURRELIG == 1 & RTNPTHY == 1] <- 1
+ })
> dat
       FID   IID CURRELIG PLASER RTNPTHY PHENO
1  fam5610 G5610        1      1       1     1
2  fam5614 G5614        1      2       2     2
3  fam5615 G5615        1      1       1     1
4  fam5618 G5618        1      1       2     1
5  fam5621 G5621        1      1       1     1
6  fam5624 G5624        1      1       2     1
7  fam5625 G5625        1      1       1     1
8  fam5628 G5628        1      2       2     2
9  fam5633 G5633        1      2       2     2
10 fam5634 G5634        1      1       1     1
11 fam5635 G5635        2      2       2    NA
12 fam5636 G5636        1      1       1     1
13 fam5641 G5641        1      1       1     1
14 fam5645 G5645        2      1       2    NA
15 fam5646 G5646        2      2       2    NA
16 fam5654 G5654        1      2       2     2
17 fam5655 G5655        1      2       2     2
18 fam5656 G5656        2      2       2    NA
19 fam5658 G5658        1      1       1     1
20 fam5659 G5659        2      2       2    NA
21 fam5660 G5660        1      1       1     1
22 fam5661 G5661        2      2       2    NA
23 fam5664 G5664        1      1       1     1
24 fam5666 G5666        1      1       1     1
25 fam5667 G5667        1      1       2     1
26 fam5670 G5670        1      1       1     1
27 fam5671 G5671        1      1       2     1
28 fam5672 G5672        1      1       2     1
29 fam5673 G5673        1      1       1     1
30 fam5680 G5680        1      2       2     2
31 fam5686 G5686        1      2       2     2
32 fam5687 G5687        1      2       2     2
33 fam5688 G5688        1      1       2     1
34 fam5693 G5693        2      1       1    NA
35 fam5695 G5695        1      1       1     1
36 fam5697 G5697        1      1       1     1
37 fam5700 G5700        1      2       2     2
38 fam5701 G5701        1      1       1     1
39 fam5706 G5706        1      1       2     1
40 fam5709 G5709        1      1       1     1
41 fam5713 G5713        1      1       1     1
42 fam5715 G5715        1      1       1     1
43 fam5718 G5718        1      1       1     1


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Sep 23, 2020 at 9:44 AM Ana Marija <[hidden email]>
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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

Jeremie Juste
In reply to this post by anikaM

Hello Ana Marija,

I cannot reproduce your error,

with a$PHENO=ifelse(a$PLASER==2 |a$RTNPTHY==2, 2, ifelse(a$CURRELIG==1 | a$RTNPTHY==1,1,NA))
For instance I have the expected PHENO=2

>     FID      IID   CURRELIG  PLASER  RTNPTHY PHENO
> 39: fam5706 G5706        1      1       2     2

In general I find nested ifelse to be difficult to work with especially
when I am tired :-). I would suggest this alternative way instead. It uses
data.table and you can investigate each step if you need to.

library(data.table)
setDT(a)
a[,PHENO:=NA]
a[PLASER==2|RTNPTHY==2,PHENO:=2]
a[is.na(PHENO)&(CURRELIG==1|RTNPTHY==1),PHENO:=1]


HTH,
Jeremie

a <- read.table(text="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",sep=",", header=TRUE)

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

anikaM
Hi Jeremie,

when I try to reproduce your code this is what I get:

> a=setDT(a)
> head(a)
       FID  IID CURRELIG PLASER RTNPTHY
1: fam0110 G110        2      2       2
2: fam0113 G113        2      2       2
3: fam0114 G114        2      2       2
4: fam0117 G117        2      2       2
5: fam0118 G118        2     NA       2
6: fam0119 G119        2      1       2
> a=a[,PHENO:=NA]
> head(a)
       FID  IID CURRELIG PLASER RTNPTHY PHENO
1: fam0110 G110        2      2       2    NA
2: fam0113 G113        2      2       2    NA
3: fam0114 G114        2      2       2    NA
4: fam0117 G117        2      2       2    NA
5: fam0118 G118        2     NA       2    NA
6: fam0119 G119        2      1       2    NA
> a=a[PLASER==2|RTNPTHY==2,PHENO:=2]
Warning message:
In `[.data.table`(a, PLASER == 2 | RTNPTHY == 2, `:=`(PHENO, 2)) :
  2.000000 (type 'double') at RHS position 1 taken as TRUE when
assigning to type 'logical' (column 6 named 'PHENO')

Please advise,
Ana

On Wed, Sep 23, 2020 at 2:48 PM Jeremie Juste <[hidden email]> wrote:

>
>
> Hello Ana Marija,
>
> I cannot reproduce your error,
>
> with a$PHENO=ifelse(a$PLASER==2 |a$RTNPTHY==2, 2, ifelse(a$CURRELIG==1 | a$RTNPTHY==1,1,NA))
> For instance I have the expected PHENO=2
>
> >     FID      IID   CURRELIG  PLASER  RTNPTHY PHENO
> > 39: fam5706 G5706        1      1       2     2
>
> In general I find nested ifelse to be difficult to work with especially
> when I am tired :-). I would suggest this alternative way instead. It uses
> data.table and you can investigate each step if you need to.
>
> library(data.table)
> setDT(a)
> a[,PHENO:=NA]
> a[PLASER==2|RTNPTHY==2,PHENO:=2]
> a[is.na(PHENO)&(CURRELIG==1|RTNPTHY==1),PHENO:=1]
>
>
> HTH,
> Jeremie
>
> a <- read.table(text="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",sep=",", header=TRUE)
>
>
>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

Jeremie Juste

Hello Ana Marija,

Apologies, the warning escaped me. When Pheno is assigned NA .
> a=a[,PHENO:=NA]
It is assigned a NA of type logical  by default. We just have to make
sure it is an NA of type numeric

> a[,PHENO:=1.0*NA]

So the full set of commands is:

library(data.table)
setDT(a)
a[,PHENO:=1.0*NA]
a[PLASER==2|RTNPTHY==2,PHENO:=2]
a[is.na(PHENO)&(CURRELIG==1|RTNPTHY==1),PHENO:=1]

Best regards,
Jeremie



Ana Marija <[hidden email]> writes:

> Hi Jeremie,
>
> when I try to reproduce your code this is what I get:
>
>> a=setDT(a)
>> head(a)
>        FID  IID CURRELIG PLASER RTNPTHY
> 1: fam0110 G110        2      2       2
> 2: fam0113 G113        2      2       2
> 3: fam0114 G114        2      2       2
> 4: fam0117 G117        2      2       2
> 5: fam0118 G118        2     NA       2
> 6: fam0119 G119        2      1       2
>> a=a[,PHENO:=NA]
>> head(a)
>        FID  IID CURRELIG PLASER RTNPTHY PHENO
> 1: fam0110 G110        2      2       2    NA
> 2: fam0113 G113        2      2       2    NA
> 3: fam0114 G114        2      2       2    NA
> 4: fam0117 G117        2      2       2    NA
> 5: fam0118 G118        2     NA       2    NA
> 6: fam0119 G119        2      1       2    NA
>> a=a[PLASER==2|RTNPTHY==2,PHENO:=2]
> Warning message:
> In `[.data.table`(a, PLASER == 2 | RTNPTHY == 2, `:=`(PHENO, 2)) :
>   2.000000 (type 'double') at RHS position 1 taken as TRUE when
> assigning to type 'logical' (column 6 named 'PHENO')
>
> Please advise,
> Ana
>
> On Wed, Sep 23, 2020 at 2:48 PM Jeremie Juste <[hidden email]> wrote:
>>
>>
>> Hello Ana Marija,
>>
>> I cannot reproduce your error,
>>
>> with a$PHENO=ifelse(a$PLASER==2 |a$RTNPTHY==2, 2, ifelse(a$CURRELIG==1 | a$RTNPTHY==1,1,NA))
>> For instance I have the expected PHENO=2
>>
>> >     FID      IID   CURRELIG  PLASER  RTNPTHY PHENO
>> > 39: fam5706 G5706        1      1       2     2
>>
>> In general I find nested ifelse to be difficult to work with especially
>> when I am tired :-). I would suggest this alternative way instead. It uses
>> data.table and you can investigate each step if you need to.
>>
>> library(data.table)
>> setDT(a)
>> a[,PHENO:=NA]
>> a[PLASER==2|RTNPTHY==2,PHENO:=2]
>> a[is.na(PHENO)&(CURRELIG==1|RTNPTHY==1),PHENO:=1]
>>
>>
>> HTH,
>> Jeremie
>>
>> a <- read.table(text="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",sep=",", header=TRUE)
>>
>>
>>
>>
>>
>>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: help with nesting if else statements

PIKAL Petr
In reply to this post by anikaM
Hi

instead of complicated ifelse construction  I would try perform the task in
several steps

# make a new column
test$new <- NA

# select CURRELIG and RTNPTHY and set new to 1
test$new[which(test$CURRELIG==1 & test$RTNPTHY==1)] <- 1

# select CURRELIG and PLASER and set new to 2
test$new[which(test$CURRELIG==1 & test$PLASER==2)] <- 2

> test
       FID   IID CURRELIG PLASER RTNPTHY new
1  fam5610 G5610        1      1       1   1
2  fam5614 G5614        1      2       2   2
3  fam5615 G5615        1      1       1   1
4  fam5618 G5618        1      1       2  NA
5  fam5621 G5621        1      1       1   1
6  fam5624 G5624        1      1       2  NA
7  fam5625 G5625        1      1       1   1
8  fam5628 G5628        1      2       2   2
9  fam5633 G5633        1      2       2   2
10 fam5634 G5634        1      1       1   1
11 fam5635 G5635        2      2       2  NA
12 fam5636 G5636        1      1       1   1
13 fam5641 G5641        1      1       1   1
14 fam5645 G5645        2      1       2  NA
15 fam5646 G5646        2      2       2  NA
16 fam5654 G5654        1      2       2   2
17 fam5655 G5655        1      2       2   2
18 fam5656 G5656        2      2       2  NA
19 fam5658 G5658        1      1       1   1
20 fam5659 G5659        2      2       2  NA

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Ana Marija
> Sent: Wednesday, September 23, 2020 6:44 PM
> To: r-help <[hidden email]>
> Subject: [R] help with nesting if else statements
>
> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.