Using if
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