Katya Mauff
2009-Mar-16 09:55 UTC
[R] Please help! How do I change the class of a numeric variable in a grouped data object to a factor?
Hi all I’m in desperate need of help. I’m working with a grouped data object, called Orthodont in the nlme package in R, and am trying to fit various models (learning methods for my thesis), but one of the variables in the object is numeric, (age) and I need it to be a factor. I’ve tried: as.factor(Orthodont$age) as.factor(as.character(Orthodont$age)) and various other things, but when I then check the class of the variables using sapply(Orthodont, data.class) I still get age as numeric… Also, sex is a factor, but for the models I’m fitting I need it to be coded as a binary variable with values (-1) for males and (+1) for females, and I don’t know how to do this either. Please help-I’m losing my mind….! ______________________________________________________________________________________________ UNIVERSITY OF CAPE TOWN This e-mail is subject to the UCT ICT policies and e-mail disclaimer published on our website at http://www.uct.ac.za/about/policies/emaildisclaimer/ or obtainable from +27 21 650 4500. This e-mail is intended only for the person(s) to whom it is addressed. If the e-mail has reached you in error, please notify the author. If you are not the intended recipient of the e-mail you may not use, disclose, copy, redirect or print the content. If this e-mail is not related to the business of UCT it is sent by the sender in the sender's individual capacity. _____________________________________________________________________________________________________ [[alternative HTML version deleted]]
Sarah Goslee
2009-Mar-18 15:33 UTC
[R] Please help! How do I change the class of a numeric variable in a grouped data object to a factor?
Well, it would have helped if you'd given us an example of what exactly you did, but here's a working solution.> Orthodont <- data.frame(age = 1:6, sex = factor(c("m", "f", "m", "f", "m", "f"))) > str(Orthodont)'data.frame': 6 obs. of 2 variables: $ age: int 1 2 3 4 5 6 $ sex: Factor w/ 2 levels "f","m": 2 1 2 1 2 1> Orthodont$age <- factor(Orthodont$age) > str(Orthodont)'data.frame': 6 obs. of 2 variables: $ age: Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6 $ sex: Factor w/ 2 levels "f","m": 2 1 2 1 2 1> Orthodont$sex <- c(1, -1)[as.numeric(Orthodont$sex)] > str(Orthodont)'data.frame': 6 obs. of 2 variables: $ age: Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6 $ sex: num -1 1 -1 1 -1 1 Note that the last one depends on the ordering of your male and female factors. Here "f" is 1 and "m" is 2, so I used c(1, -1) - the desired values for "f" and "m" in numeric order by factor. Sarah On Mon, Mar 16, 2009 at 5:55 AM, Katya Mauff <Katya.Mauff at uct.ac.za> wrote:> Hi all > > I?m in desperate need of help. I?m working with a grouped data object, called Orthodont in the nlme package in R, and am trying to fit various models (learning methods for my thesis), but one of the variables in the object is numeric, (age) and I need it to be a factor. I?ve tried: as.factor(Orthodont$age) > as.factor(as.character(Orthodont$age)) > > and various other things, but when I then check the class of the variables using > sapply(Orthodont, data.class) > I still get age as numeric? > > Also, sex is a factor, but for the models I?m fitting I need it to be coded as a binary variable with values (-1) for males and (+1) for females, and I don?t know how to do this either. Please help-I?m losing my mind?.! > > >-- Sarah Goslee http://www.functionaldiversity.org