[R] Create variable with AND IF statement

David Winsemius dwinsemius at comcast.net
Mon Jan 2 22:54:33 CET 2012


On Jan 2, 2012, at 4:22 PM, David Stevens wrote:

> Could it be

NO. You are not reading the documentation for "if" carefully enough,  
despite several efforts to point you in the right direction. You  
cannot make literal transliterations of SPSS syntax work in the manner  
you imagine.

-- 
David.
>
> if(variable1.fac == 0 & variable2.num == 0) {variable3 = 1}         #
> brackets {} aren't strictly required for a one liner.
> if(variable1.fac == 0 & variable2.num >= 0) {variable3 = 2}         #
> brackets {} aren't strictly required for a one liner.
> if(variable1.fac == 1 & variable2.num == 0) {variable3 = 3}         #
> brackets {} aren't strictly required for a one liner.
> if(variable1.fac == 1 & variable2.num >= 1) {variable3 = 4}         #
> brackets {} aren't strictly required for a one liner.
>
> On 1/2/2012 2: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.
>>
>> 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 (&, |)
>> https://stat.ethz.ch/pipermail/r-help/2008-November/178808.html  
>> looked
>> promising but didn't do what I had intended
>>
>> 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.
>
> 	[[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