[R] help calculating variable based on factor level of another

Ivan Calandra ivan.calandra at uni-hamburg.de
Thu May 27 16:16:52 CEST 2010


I'm really confused about your question. For many reasons:
- because I'm a beginner myself?
- what are you trying to do with x1? You define the levels of x1, but 
you don't give any values to it. Which could (also) explain why your 
code does not work
- your if...else statement is weird for me. I think it should more look 
like: if(x1=="something") {x2 <- 1} else {...}
Curly braces are necessary when you have more than one statement as 
expression. See ?"if" and ?ifelse.
- what are you trying to do? it looks like you're trying to recode the 
factors. Why? Factors are already stored as integers in R. Maybe some 
more insights into what you're trying to do at the large scale would help.


Le 5/27/2010 15:59, Simon Kiss a écrit :
> Dear colleagues,
> I want to calculate the value of x2 based on the value of x1.  x1 is a
> factor with three separate levels. I want to make sure that missing
> values remain as NA in X2, but non-missing values take on a value of
> either 0  or 1 dependending on the value in x1.
> This is the code I'm working with...Can any one help?
> I've seen some other requests on a topic like this, but not using 
> factors with strings as levels; only with numeric variables.
> Simon
> x1<-factor(levels="social and cultural specialists", "labour",
> "salariat")
> x2<-if(x1==c("social and cultural specialists")) "1" elseif (x1==NA)
> "NA" else "0"
> *********************************
> Simon J. Kiss, PhD
> SSHRC and DAAD Post-Doctoral Fellow
> John F. Kennedy Institute of North America Studies
> Free University of Berlin
> Lansstraße 7-9
> 14195 Berlin, Germany
> Cell: +49 (0)1525-300-2812,
> Web: http://www.jfki.fu-berlin.de/index.html
> ______________________________________________
> 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.

PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de


More information about the R-help mailing list