[R] Using if
jim holtman
jholtman at gmail.com
Tue Nov 20 20:27:29 CET 2012
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.
More information about the R-help
mailing list