[R] converting a factor in numeric values with direct control of the numeric values
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Sat Jan 17 18:46:31 CET 2009
ronggui wrote:
> I think you have to recode the derived variable of as.numeric(z).
The easiest way is probably to use indexing:
> z <- c("male", "male", "female")
> z <- factor(z)
> c(5,3)[z]
[1] 3 3 5
or, slightly more foolproof
> c(male=3,female=5)[as.character(z)]
male male female
3 3 5
>
> On Sun, Jan 18, 2009 at 12:40 AM, Jörg Groß <joerg at licht-malerei.de> 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.
>>
>
>
>
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list