Dear R users, As a new comer to R, I would like to create a new variable using if statements but don't know how to do it. Basically, I have two variables (EvHint and MinTex). I want to create a third variable called RiskTest. In SPSS, my syntax would look like Compute RiskTest=0. if (EvHint=1 & MinTex=1) RiskTest=1. Question: How do I do this with R? My Data EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) Thanks, V [[alternative HTML version deleted]]
?ifelse Quite distinct from if () {} else {}. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Virgile Capo-Chichi <vcapochichi at gmail.com> wrote:>Dear R users, > >As a new comer to R, I would like to create a new variable using if >statements but don't know how to do it. Basically, I have two variables >(EvHint and MinTex). I want to create a third variable called RiskTest. > >In SPSS, my syntax would look like > >Compute RiskTest=0. >if (EvHint=1 & MinTex=1) RiskTest=1. > >Question: How do I do this with R? > >My Data > >EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) >MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) > >Thanks, V > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >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.
Hi, Thanks for providing a reproducible example. On Tue, Nov 20, 2012 at 2:08 PM, Virgile Capo-Chichi <vcapochichi at gmail.com> wrote:> Dear R users, > > As a new comer to R, I would like to create a new variable using if > statements but don't know how to do it. Basically, I have two variables > (EvHint and MinTex). I want to create a third variable called RiskTest. > > In SPSS, my syntax would look like > > Compute RiskTest=0. > if (EvHint=1 & MinTex=1) RiskTest=1. > > Question: How do I do this with R? > > My Data > > EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) > MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0)In this case, RiskTest <- EvHint * MinTex In the more general case, RiskTest <- ifelse(EvHint == 1 & MinTex == 1, 1, 0) Note that the test for equality is == and not You should probably read the Intro to R that came with your installation and is available online. -- Sarah Goslee http://www.functionaldiversity.org
> In SPSS, my syntax would look like > > Compute RiskTest=0. > if (EvHint=1 & MinTex=1) RiskTest=1.RiskTest <- EvHint == 1 & MinTex == 1 will create a logical vector, one with values TRUE and FALSE. If you really want 1 and 0 then add RiskTest <- as.integer(RiskTest), but using logicals instead of 0/1 variables seems more logical to me (then you can get rid of all those ==1's). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Virgile Capo-Chichi > Sent: Tuesday, November 20, 2012 11:09 AM > To: r-help at r-project.org > Subject: [R] Using if > > Dear R users, > > As a new comer to R, I would like to create a new variable using if > statements but don't know how to do it. Basically, I have two variables > (EvHint and MinTex). I want to create a third variable called RiskTest. > > In SPSS, my syntax would look like > > Compute RiskTest=0. > if (EvHint=1 & MinTex=1) RiskTest=1. > > Question: How do I do this with R? > > My Data > > EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) > MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) > > Thanks, V > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Try this (convert the logical TRUE to the value 1):> EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) > MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) > > RiskTest <- (EvHint == 1 & MinTex == 1) + 0L > rbind(EvHint, MinTex, RiskTest)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] EvHint 0 0 0 1 0 1 1 0 1 1 1 1 1 0 1 0 MinTex 0 0 1 1 0 1 1 0 0 0 0 1 0 0 0 0 RiskTest 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 0 [,17] [,18] [,19] [,20] EvHint 0 1 0 0 MinTex 0 0 1 0 RiskTest 0 0 0 0>On Tue, Nov 20, 2012 at 2:08 PM, Virgile Capo-Chichi <vcapochichi at gmail.com> wrote:> Dear R users, > > As a new comer to R, I would like to create a new variable using if > statements but don't know how to do it. Basically, I have two variables > (EvHint and MinTex). I want to create a third variable called RiskTest. > > In SPSS, my syntax would look like > > Compute RiskTest=0. > if (EvHint=1 & MinTex=1) RiskTest=1. > > Question: How do I do this with R? > > My Data > > EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) > MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) > > Thanks, V > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
HI, May be this: RiskTest<-ifelse(EvHint==1 & MinTex==1,1,0) RiskTest # [1] 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 A.K. ----- Original Message ----- From: Virgile Capo-Chichi <vcapochichi at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, November 20, 2012 2:08 PM Subject: [R] Using if Dear R users, As a new comer to R, I would like to create a new variable using if statements but don't know how to do it. Basically, I have two variables (EvHint and MinTex). I want to create a third variable called RiskTest. In SPSS, my syntax would look like Compute RiskTest=0. if (EvHint=1 & MinTex=1) RiskTest=1. Question: How do I do this with R? My Data EvHint<-c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0) MinTex<-c(0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0) Thanks, V ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.