[R] Create variable with AND IF statement
ONKELINX, Thierry
Thierry.ONKELINX at inbo.be
Mon Jan 2 16:43:58 CET 2012
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 op 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 op r-project.org [mailto:r-help-bounces op r-project.org] Namens Richard Kolodziej
Verzonden: maandag 2 januari 2012 10:11
Aan: r-help op 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 op 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.
More information about the R-help
mailing list