Message-ID: <E66794E69CFDE04D9A70842786030B930CE36914@PA-MBX04.na.tibco.com>
Date: 2012-11-20T19:21:31Z
From: William Dunlap
Subject: Using if
In-Reply-To: <CANTPK-+GO_JoGVA5tm1X4C8KGTxuaFeUhppaaJuHiVi58WCozw@mail.gmail.com>
> 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.