[R] if else statement adjustemtn

Rasmus Liland jr@| @end|ng |rom po@teo@no
Mon Jun 15 13:27:50 CEST 2020

On 2020-06-13 19:09 +1000, Jim Lemon wrote:
> Right, back from shopping. Since you have fourteen rows containing NAs
> and you only want seven, we can infer that half of them must go. As
> they are neatly divided into seven rows in which only one NA appears
> and seven in which two stare meaninglessly out at us. I will assume
> that the latter are the ones to be discarded. As your condition for
> calculating "pheno" stated that a 2 in either FLASER or PLASER should
> result in a 2 in pheno, the following statement closely conforms to
> that:
> b<-read.table(text="FID   IID FLASER PLASER
>   fam1837 G1837      1     NA
>   fam2410 G2410     NA     NA
>   fam2838 G2838     NA      2
>   fam3367 G3367      1     NA
>   fam3410 G3410      1     NA
>   fam3492 G3492      1     NA
>   fam0911  G911     NA     NA
>   fam3834 G3834      2     NA
>   fam4708 G4708     NA      2
>   fam5162 G5162     NA     NA
>   fam5274 G5274     NA     NA
>   fam0637  G637     NA     NA
>   fam0640  G640     NA     NA
>   fam0743  G743     NA     NA
>   fam0911  G911     NA     NA",
>   header=TRUE,stringsAsFactors=FALSE)
> b$pheno<-ifelse(b$FLASER == 2 | b$PLASER == 2,2,1)
> # use the valid FLASER values when PLASER is NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$FLASER),
>  b[is.na(b$pheno),]$FLASER,NA)
> # use the valid PLASER values when FLASER if NA
> b[is.na(b$pheno),]$pheno<-ifelse(!is.na(b[is.na(b$pheno),]$PLASER),
>  b[is.na(b$pheno),]$PLASER,NA)
> b
> I could write that mess in one straitjacket of conditional statements
> but my brain hurts enough.

I think this answer is billiant!

Here's a picture of my laptop screen out in my local park 
yesterday as I was trying to figure this out


I have nothing more to add at the moment :)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200615/8cba0334/attachment.sig>

More information about the R-help mailing list