[R] Using if

William Dunlap wdunlap at tibco.com
Tue Nov 20 20:21:31 CET 2012


> 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.




More information about the R-help mailing list