Simon Kiss
2010-May-27 13:59 UTC
[R] help calculating variable based on factor level of another
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
Ivan Calandra
2010-May-27 14:16 UTC
[R] help calculating variable based on factor level of another
Hi,
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.
HTH,
Ivan
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.
>
--
Ivan CALANDRA
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
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Joshua Wiley
2010-May-27 14:30 UTC
[R] help calculating variable based on factor level of another
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/
Apparently Analagous Threads
- Grouping and stacking bar plot for categorical variables
- Problem with recode -Error in parse(text = range[[1]][1]) : unexpected end of input in " c(0"
- generate irregular series of dates
- Finding different hues for a mosaic plot compatible with grayscale printing
- Comparing a 4-point and 5-point Likert scale