Skip to content

Create variable with AND IF statement

8 messages · ONKELINX, Thierry, MacQueen, Don, David Stevens +2 more

#
On Jan 2, 2012, at 4:11 AM, Richard Kolodziej wrote:

            
Do you not find the 'ifelse' function described in "An Introduction to  
R" in the section regarding "if".
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.
(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) )
#
Dear Richard,

You can do this with some nested ifelse statements.
Assuming variable2.num has only positive integers and variable1.fac is only 0 or 1

Variable3 <- ifelse(variable1.fac == 0, ifelse(variable2.num == 0, 1, 2), ifelse(variable2.num == 0; 3; 4))

A more fancy solution

as.numeric(factor(variable1.fac):factor(ifelse(variable2.num == 0, 0, 1)))

Best regards,

Thierry

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey


-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Richard Kolodziej
Verzonden: maandag 2 januari 2012 10:11
Aan: r-help at r-project.org
Onderwerp: [R] Create variable with AND IF statement

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


______________________________________________
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.
#
On Jan 2, 2012, at 10:42 AM, David Winsemius wrote:

            
Well that should be filed under embarrassing contributions. Try instead:

  var3 <- as.numeric( interaction( variable1.fac == 1 , variable2.num  
 >= 1) )
David Winsemius, MD
West Hartford, CT
#
I usually do this kind of thing like this:

variable3 <- rep(1,length(variable1.fac))
variable3[ variable1.fac == 0 & variable2.num >= 1 ] <- 2
variable3[ variable1.fac == 1 & variable2.num == 0 ] <- 3
variable3[ variable1.fac == 1 & variable2.num >= 1 ] <- 4

This approach is easy to read and understand, and I would (personally)
consider it analogous to the SPSS approach. This approach does require
that the variables all have the same length -- which may be intrinsically
built in to the SPSS data structure, but is not guaranteed in R (unless
all of the variables are contained in a data frame).

The key concept is that in R, the if() function is for logical objects of
length=1, only. The ifelse() function is for logical objects of length >
1. In R terminology, we would say that ifelse() is a vectorized function,
but if() is not. The SPSS if function appears to behave in a vectorized
manner.

Although nested ifelse() functions can be used in this case, I find them
harder to read.


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

            
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.
#
Hello,

thanks for the quick answers! Some answers were more useful to me than
others but I really appreciate everybodys effort, reading and
answering my question.

I will try Dons approach because as he said, it looks quite readable.
I'll need to take a closer look on the nested ifelse statements to
understand and use them.

What I learned from asking this question is, that I should have stated
my problem far more precisely. I should have shown you what I tried
and what error messages appeared.

Thanks everybody,
Richard