[R] how to create a new column from two columns with conditions

Ege Rubak rub@k @end|ng |rom m@th@@@u@dk
Wed Apr 29 21:35:27 CEST 2020


Or if you prefer not to load an entire suite of packages to do such a
simple task you could use

b$PHENO <- ifelse(...)

or in this specific case it seems sufficient to do

b$PHENO <- pmax(b$FLASER, b$PLASER)

/Ege

On Wed, 2020-04-29 at 15:30 -0400, Patrick (Malone Quantitative) wrote:
> If you don't mind using tidyverse, you can do this easily with
> if_else.
> 
> b$PHENO<-if_else(...
> 
> 
> 
> On Wed, Apr 29, 2020 at 3:21 PM Ana Marija <
> sokovic.anamarija using gmail.com>
> wrote:
> 
> > Hello,
> > 
> > I have a data frame like this:
> > 
> > > head(b)
> > 
> >        FID          IID FLASER PLASER
> > 1: fam1000 G1000      1      1
> > 2: fam1001 G1001      1      1
> > 3: fam1003 G1003      1      2
> > 4: fam1005 G1005      1      1
> > 5: fam1009 G1009      2      1
> > 6: fam1052 G1052      1      1
> > ...
> > 
> > My conditions for creating a new column PHENO would be this:
> > 
> > if FLASER or PLASER =2 then PHENO=2
> > otherwise PHENO=1
> > 
> > so result would look like this:
> > 
> > > head(b)
> > 
> >        FID          IID FLASER PLASER PHENO
> > 1: fam1000 G1000      1      1            1
> > 2: fam1001 G1001      1      1            1
> > 3: fam1003 G1003      1      2            2
> > 4: fam1005 G1005      1      1            1
> > 5: fam1009 G1009      2      1             2
> > 6: fam1052 G1052      1      1             1
> > ...
> > 
> > Thanks
> > 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.
> > 
> 
> 
-- 
Ege Rubak, Associate Professor,
Department of Mathematical Sciences, Aalborg University
Skjernvej 4A, 9220 Aalborg East, Denmark
Phone: (+45)99408861
Mobile: (+45)30230252
Email: rubak using math.aau.dk


More information about the R-help mailing list