[R] Create variable with AND IF statement
David Winsemius
dwinsemius at comcast.net
Mon Jan 2 16:42:12 CET 2012
On Jan 2, 2012, at 4:11 AM, Richard Kolodziej wrote:
> Hello,
>
> 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
> stuck
> 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
> variables:
> * 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
> statement
> that doesn't produce an error or not intended solutions.
>
> "An Introduction to R" is really unhelpful with this problem and I
> wouldn't
> 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
> my
> 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
correct approach.
> 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 =
0) )
--
David
>
> Thanks in advance,
> Richard
>
> [[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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list