Skip to content
Back to formatted view

Raw Message

Message-ID: <CAB7CB2F-878C-428A-8433-7EDC80B48052@comcast.net>
Date: 2009-01-17T17:22:18Z
From: David Winsemius
Subject: converting a factor in numeric values with direct control of the numeric values
In-Reply-To: <87644ED2-53CD-4ABA-A2A4-34EC60498508@licht-malerei.de>

ifelse is your friend:

 > z <- c("male", "male", "female")
 >  z <- as.factor(z)
 >
 >  z <- ifelse(z == "male", 3, 5)

 > z
[1] 3 3 5
 > class(z)
[1] "numeric"
 >

--  
David Winsemius

On Jan 17, 2009, at 11:40 AM, J?rg Gro? wrote:

> Hi,
>
> I know how to convert a factor-variable into a numeric variable via  
> as.numeric().
>
> But how can I control the values that are assigned?
>
>
> For example, I have this factor-variable:
>
>
> z <- c("male", "male", "female")
> z <- as.factor(z)
>
>
> And I want to convert male in 3 and female into the numeric value 5  
> (just for the example)
>
> so that I get:
>
> [1] 3 3 5
>
>
> Thanks for any help!
>
> ______________________________________________
> 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.