[R] Create variable with AND IF statement
dwinsemius at comcast.net
Mon Jan 2 16:42:12 CET 2012
On Jan 2, 2012, at 4:11 AM, Richard Kolodziej wrote:
> I'm using SPSS at work but really would like to switch to R. Right
> now I'm
> trying to learn R in reproducing calculations I did with SPSS but am
> with something that is quite simple and comprehensible in SPSS-Syntax:
> IF (variable1.fac = 0 AND variable2.num = 0) variable3=1.
> IF (variable1.fac = 0 AND variable2.num >= 1) variable3=2.
> IF (variable1.fac = 1 AND variable2.num = 0) variable3=3.
> IF (variable1.fac = 1 AND variable2.num >= 1) variable3=4.
> I want to create four different groups out of different conditions
> of two
> * variable1.fac is a factor coded with 0 and 1
> * variable2.num is a numerical variable with only whole numbers
> My problem with R is that I can't find a way to use AND in an IF
> that doesn't produce an error or not intended solutions.
> "An Introduction to R" is really unhelpful with this problem and I
> have written here, if I didn't have searched for the answer.
Do you not find the 'ifelse' function described in "An Introduction to
R" in the section regarding "if".
> http://tolstoy.newcastle.edu.au/R/help/05/09/12136.html was helpful in
> understanding how the IF statement is written in R but didn't answer
> question how to add an usable AND (&, |)
Not the best answer I have ever seen in the archives. But if you had
followed up by reading further in the thread you would have found the
> https://stat.ethz.ch/pipermail/r-help/2008-November/178808.html looked
> promising but didn't do what I had intended
(I think the results of interaction( variable1.fac = 0 , variable2.num
= 0) should have dome very nicely, but if you wanted them a naked
numbers then this would have also givne satisfaction:
var3 <- as.numeric( interaction( variable1.fac = 0 , variable2.num =
> Thanks in advance,
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help