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
Rui Barradas
2020-Apr-29 21:10 UTC
[R] how to create a new column from two columns with conditions
Hello, Here is another way. The condition returns FALSE/TRUE or 0/1. Add 1 to get the expected result. It has the advantage of being faster. b$PHENO <- (b$FLASER == 2 | b$PLASER == 2) + 1L Hope this helps, Rui Barradas ?s 20:42 de 29/04/20, Ana Marija escreveu:> 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 > > ______________________________________________ > 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. >
Ana Marija
2020-Apr-29 21:44 UTC
[R] how to create a new column from two columns with conditions
Hi Rui, thanks for getting back to me so I tried your method and I got:> sum(b$PHENO==2, na.rm=T)[1] 828> sum(b$PHENO==1, na.rm=T)[1] 859 Can you please tell me if b$PHENO <- (b$FLASER == 2 | b$PLASER == 2) + 1L just assigns PHENO=2 if b$FLASER == 2 | b$PLASER == 2 and everything else is 1? Please see how my data looks like:> sum(b$FLASER==2, na.rm=T)[1] 92> sum(b$FLASER==1, na.rm=T)[1] 1533> sum(b$PLASER==1, na.rm=T)[1] 850> sum(b$PLASER==2, na.rm=T)[1] 806> dim(b)[1] 1698 5> unique(b$FLASER)[1] 1 3 2 NA> unique(b$PLASER)[1] 1 2 3 NA On Wed, Apr 29, 2020 at 4:10 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:> > Hello, > > Here is another way. The condition returns FALSE/TRUE or 0/1. Add 1 to > get the expected result. > It has the advantage of being faster. > > b$PHENO <- (b$FLASER == 2 | b$PLASER == 2) + 1L > > > Hope this helps, > > Rui Barradas > > ?s 20:42 de 29/04/20, Ana Marija escreveu: > > 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 > > > > ______________________________________________ > > 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. > >
Ana Marija
2020-Apr-29 22:20 UTC
[R] how to create a new column from two columns with conditions
Thank you so much, numbers add up now! On Wed, Apr 29, 2020 at 5:09 PM <cpolwart at chemo.org.uk> wrote:> > While I've sent you a maths way to do it, > > I'd probably take this approach; > > # set b$pheno to 1 as default > > b$pheno <- 1 > > # set the flaser > > b$pheno[b$flaser == 2] <- 2 > > #set the plaser > > b$pheno[b$plaser == 2] <- 2 > > > On 29 Apr 2020 22:44, Ana Marija <sokovic.anamarija at gmail.com> wrote: > > Hi Rui, > > thanks for getting back to me > so I tried your method and I got: > > sum(b$PHENO==2, na.rm=T) > [1] 828 > > sum(b$PHENO==1, na.rm=T) > [1] 859 > > Can you please tell me if > b$PHENO <- (b$FLASER == 2 | b$PLASER == 2) + 1L > > just assigns PHENO=2 if b$FLASER == 2 | b$PLASER == 2 and everything else is 1? > > Please see how my data looks like: > > sum(b$FLASER==2, na.rm=T) > [1] 92 > > sum(b$FLASER==1, na.rm=T) > [1] 1533 > > sum(b$PLASER==1, na.rm=T) > [1] 850 > > sum(b$PLASER==2, na.rm=T) > [1] 806 > > dim(b) > [1] 1698 5 > > unique(b$FLASER) > [1] 1 3 2 NA > > unique(b$PLASER) > [1] 1 2 3 NA > > On Wed, Apr 29, 2020 at 4:10 PM Rui Barradas <ruipbarradas at sapo.pt> wrote: > > > > Hello, > > > > Here is another way. The condition returns FALSE/TRUE or 0/1. Add 1 to > > get the expected result. > > It has the advantage of being faster. > > > > b$PHENO <- (b$FLASER == 2 | b$PLASER == 2) + 1L > > > > > > Hope this helps, > > > > Rui Barradas > > > > ?s 20:42 de 29/04/20, Ana Marija escreveu: > > > 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 > > > > > > ______________________________________________ > > > 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. > > > > > ______________________________________________ > 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. > >