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]]
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
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 [[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.
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 -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 1/2/12 1:11 AM, "Richard Kolodziej" <Richard.Kolodziej at web.de> 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.
Could it be 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@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]]