Re: [R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread Peter Dalgaard
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

Re: [R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread Charles C. Berry
On Sat, 17 Jan 2009, Charles C. Berry wrote: On Sat, 17 Jan 2009, 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", "mal

Re: [R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread David Winsemius
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 variabl

Re: [R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread Charles C. Berry
On Sat, 17 Jan 2009, 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 con

Re: [R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread ronggui
BTW, a better way to convert factor into a numeric variable is to use unclass(). On Sun, Jan 18, 2009 at 12: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 exam

Re: [R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread ronggui
I think you have to recode the derived variable of as.numeric(z). On Sun, Jan 18, 2009 at 12: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

[R] converting a factor in numeric values with direct control of the numeric values

2009-01-17 Thread Jörg Groß
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 nume