Message-ID: <CAM_vjumK8W7oXAzY7D=5HjpB8GAhu5q4tnta2P5vS_aRPZMwYg@mail.gmail.com>
Date: 2012-11-20T19:21:26Z
From: Sarah Goslee
Subject: Using if
In-Reply-To: <CANTPK-+GO_JoGVA5tm1X4C8KGTxuaFeUhppaaJuHiVi58WCozw@mail.gmail.com>
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