[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 numeric

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ß jo...@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

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ß jo...@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

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 convert

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 variable via

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, male,

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 3 3