Jörg Groß
2009-Jan-17 16:40 UTC
[R] converting a factor in numeric values with direct control of the numeric values
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!
ronggui
2009-Jan-17 16:44 UTC
[R] converting a factor in numeric values with direct control of the numeric values
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? <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. >-- HUANG Ronggui, Wincent Tel: (00852) 3442 3832 PhD Candidate Dept of Public and Social Administration City University of Hong Kong Homepage: http://ronggui.huang.googlepages.com/
ronggui
2009-Jan-17 16:46 UTC
[R] converting a factor in numeric values with direct control of the numeric values
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? <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. >-- HUANG Ronggui, Wincent Tel: (00852) 3442 3832 PhD Candidate Dept of Public and Social Administration City University of Hong Kong Homepage: http://ronggui.huang.googlepages.com/
Charles C. Berry
2009-Jan-17 17:12 UTC
[R] converting a factor in numeric values with direct control of the numeric values
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 male in 3 and female into the numeric value 5 (just for > the example) > > so that I get: > > [1] 3 3 5Like this:> z <- c("male", "male", "female") > z <- as.factor(z) > num.codes[ levels(z) ][ z ]male male female 3 3 5>or if you want to avoid the names():> z.to.num <- unname(num.codes)[ match(levels(z),names(num.codes)) ] > z.to.num[ z ][1] 3 3 5>See ?as.character ?match ?levels HTH, Chuck> > > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
David Winsemius
2009-Jan-17 17:22 UTC
[R] converting a factor in numeric values with direct control of the numeric values
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.
Charles C. Berry
2009-Jan-17 17:32 UTC
[R] converting a factor in numeric values with direct control of the numeric values
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", "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 > > > Like this: >> z <- c("male", "male", "female") >> z <- as.factor(z)Oops! I forgot to copy this line> num.codes <- c(male = 3, female = 5 )>> num.codes[ levels(z) ][ z ] > male male female > 3 3 5 >> > or if you want to avoid the names(): > >> z.to.num <- unname(num.codes)[ match(levels(z),names(num.codes)) ] >> z.to.num[ z ] > [1] 3 3 5 >> > > > See > > ?as.character > ?match > ?levels > > HTH, > > Chuck > >> >> >> 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. >> > > Charles C. Berry (858) 534-2098 > Dept of Family/Preventive > Medicine > E mailto:cberry at tajo.ucsd.edu UC San Diego > http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901 > > >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901