Skip to content
Prev 385777 / 398503 Next

help with nesting if else statements

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
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)
Message-ID: <87tuvoxoig.fsf@gmail.com>
In-Reply-To: <CAF9-5jP5eEs9qPccO4c4WZNhPSSSFwS7kr8cF=pobf4tsqgPDw@mail.gmail.com> (Ana Marija's message of "Wed, 23 Sep 2020 11:43:42 -0500")