Re: [R] how to create a new column with conditions
On April 8, 2020 1:17:38 PM PDT, Ana Marija wrote: | Hi, | | I have a data frame like this: Or ... *drum-roll* ... you could use plain old indexing. Have a look: a <- "FIDLASER2 CURRELIGPLASER RTNPTHY fam1000_G1000 1 1 1 1 fam1001_G1001 1 1 1 1 fam1003_G1003 2 1 2 2 fam1005_G1005 1 1 1 2 fam1009_G1009 1 1 1 2 fam1052_G1052 1 1 1 2" a <- read.delim(text=a) a$PHENO <- -9 a[a$CURRELIG==1 & a$RTNPTHY==1,"PHENO"] <- 1 a[a$PLASER==2,"PHENO"] <- 2 Best, Rasmus __ R-help@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.
Re: [R] how to create a new column with conditions
>I would like to create a new column called PHENO which would satisfy >these >conditions: > >if CURRELIG=1 and RTNPTHY=1 than PHENO=1 >if PLASER=2 than PHENO=2 >otherwise is -9 I assume that if CURRELIG==1 and RNPTHY==1 and PLASER==2 then PHENO should be 1. Or should that case flag a data error?. In the former case try a$PHENO <- with(a, { tmp <- rep(-9, length(CURRELIG)), tmp[CURRELIG==1 & RTNPTHY==1] <- 1 tmp[PLASER==2] <- 2 tmp}) In the latter case add some calls to stop() or warning() if any of the "impossible" cases are seen. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Apr 8, 2020 at 1:32 PM Jeff Newmiller wrote: > Now that you have been shown how to do this, post your (non-working) code > next time. And configure your email program to send plain text so we will > see what you saw. > > a$PHENO <- ifelse( a$CURRELIG==1 > & a$RTNPTHY==1 > , 1 > , ifelse( a$PLASER==2 > , 2 > , -9 ) ) > > or > > a$PHENO <- with( a >, ifelse( CURRELIG==1 >& RTNPTHY==1 >, 1 >, ifelse( PLASER==2 >, 2 >, -9 ) ) ) > > > On April 8, 2020 1:17:38 PM PDT, Ana Marija > wrote: > >Hi, > > > >I have a data frame like this: > > > >> head(a) > >FID LASER2 CURRELIG PLASER RTNPTHY > >1 fam1000_G1000 11 1 1 > >2 fam1001_G1001 11 1 1 > >3 fam1003_G1003 21 2 2 > >4 fam1005_G1005 11 1 2 > >5 fam1009_G1009 11 1 2 > >6 fam1052_G1052 11 1 2 > >... > > > >I would like to create a new column called PHENO which would satisfy > >these > >conditions: > > > >if CURRELIG=1 and RTNPTHY=1 than PHENO=1 > >if PLASER=2 than PHENO=2 > >otherwise is -9 > > > >Thanks > >Ana > > > > [[alternative HTML version deleted]] > > > >__ > >R-help@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. > > -- > Sent from my phone. Please excuse my brevity. > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] how to create a new column with conditions
Now that you have been shown how to do this, post your (non-working) code next time. And configure your email program to send plain text so we will see what you saw. a$PHENO <- ifelse( a$CURRELIG==1 & a$RTNPTHY==1 , 1 , ifelse( a$PLASER==2 , 2 , -9 ) ) or a$PHENO <- with( a , ifelse( CURRELIG==1 & RTNPTHY==1 , 1 , ifelse( PLASER==2 , 2 , -9 ) ) ) On April 8, 2020 1:17:38 PM PDT, Ana Marija wrote: >Hi, > >I have a data frame like this: > >> head(a) >FID LASER2 CURRELIG PLASER RTNPTHY >1 fam1000_G1000 11 1 1 >2 fam1001_G1001 11 1 1 >3 fam1003_G1003 21 2 2 >4 fam1005_G1005 11 1 2 >5 fam1009_G1009 11 1 2 >6 fam1052_G1052 11 1 2 >... > >I would like to create a new column called PHENO which would satisfy >these >conditions: > >if CURRELIG=1 and RTNPTHY=1 than PHENO=1 >if PLASER=2 than PHENO=2 >otherwise is -9 > >Thanks >Ana > > [[alternative HTML version deleted]] > >__ >R-help@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. -- Sent from my phone. Please excuse my brevity. __ R-help@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] how to create a new column with conditions
Hi, I have a data frame like this: > head(a) FID LASER2 CURRELIG PLASER RTNPTHY 1 fam1000_G1000 11 1 1 2 fam1001_G1001 11 1 1 3 fam1003_G1003 21 2 2 4 fam1005_G1005 11 1 2 5 fam1009_G1009 11 1 2 6 fam1052_G1052 11 1 2 ... I would like to create a new column called PHENO which would satisfy these conditions: if CURRELIG=1 and RTNPTHY=1 than PHENO=1 if PLASER=2 than PHENO=2 otherwise is -9 Thanks Ana [[alternative HTML version deleted]] __ R-help@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.