Sorry that last bit of code was redundant, it should be: x2 <- ifelse(x1=="social and cultural specialists", "1", "0")
ifelse() leaves NAs by default so no need to manually do it. On Thu, May 27, 2010 at 7:30 AM, Joshua Wiley <jwiley.ps...@gmail.com> wrote: > Dear Simon, > > It is a bit hard to tell what you want without a reproducible example, > below is my guess. I created x1 as a factor with some data in it > based off of what I think you are working with and then x2 from x1. > > ################################ > data <- c("social and cultural specialists", "labour","salariat") > x1<-factor(rep(data,3), levels=data, labels=data) > x1[10] <- NA > x2 <- ifelse(x1=="social and cultural specialists", "1", > ifelse(is.na(x1), "NA", "0")) > x2 > ################################ > > Hope that helps, > > Josh > > On Thu, May 27, 2010 at 6:59 AM, Simon Kiss <sjk...@gmail.com> wrote: >> 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@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. >> > > > > -- > Joshua Wiley > Senior in Psychology > University of California, Riverside > http://www.joshuawiley.com/ > -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ ______________________________________________ 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.