Ana Marija
2020-Apr-29 19:19 UTC
[R] how to create a new column from two columns with conditions
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 ...
Patrick (Malone Quantitative)
2020-Apr-29 19:30 UTC
[R] how to create a new column from two columns with conditions
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 at 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 at 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. >-- Patrick S. Malone, Ph.D., Malone Quantitative NEW Service Models: http://malonequantitative.com He/Him/His [[alternative HTML version deleted]]
Ege Rubak
2020-Apr-29 19:35 UTC
[R] how to create a new column from two columns with conditions
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 at 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 at 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 at math.aau.dk
Ivan Krylov
2020-Apr-29 19:36 UTC
[R] how to create a new column from two columns with conditions
On Wed, 29 Apr 2020 14:19:18 -0500 Ana Marija <sokovic.anamarija at gmail.com> wrote:> My conditions for creating a new column PHENO would be this: > > if FLASER or PLASER =2 then PHENO=2 > otherwise PHENO=1On Wed, 29 Apr 2020 15:30:45 -0400 "Patrick (Malone Quantitative)" <malone at malonequantitative.com> wrote:> If you don't mind using tidyverse, you can do this easily with > if_else....and if you want to stay with base R, you can use the ifelse function. -- Best regards, Ivan
Ana Marija
2020-Apr-29 19:42 UTC
[R] how to create a new column from two columns with conditions
Thanks, I did this: b$PHENO<- ifelse(b$FLASER ==2 | b$PLASER ==2, 2, 1) On Wed, Apr 29, 2020 at 2:36 PM Ivan Krylov <krylov.r00t at gmail.com> wrote:> > On Wed, 29 Apr 2020 14:19:18 -0500 > Ana Marija <sokovic.anamarija at gmail.com> wrote: > > > My conditions for creating a new column PHENO would be this: > > > > if FLASER or PLASER =2 then PHENO=2 > > otherwise PHENO=1 > > On Wed, 29 Apr 2020 15:30:45 -0400 > "Patrick (Malone Quantitative)" <malone at malonequantitative.com> wrote: > > > If you don't mind using tidyverse, you can do this easily with > > if_else. > > ...and if you want to stay with base R, you can use the ifelse > function. > > -- > Best regards, > Ivan