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

Simon Kiss sjkiss at gmail.com
Thu May 27 15:59:08 CEST 2010

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.

x1<-factor(levels="social and cultural specialists", "labour",

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

More information about the R-help mailing list