[R] help calculating variable based on factor level of another
Joshua Wiley
jwiley.psych at gmail.com
Thu May 27 16:30:31 CEST 2010
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 <sjkiss at 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 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.
>
--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/
More information about the R-help
mailing list