[R] Help with "recode" and "factor" functions

John Fox jfox at mcmaster.ca
Tue Oct 24 14:00:45 CEST 2006


Dear Chris,

I hesitate to answer a question arising from an exam -- you should probably
address the question to the person who set the exam -- but it doesn't hurt,
I think, to point out the following: The recode function that you're using
is in the car package, which is associated with a book (see ?car) that has
several examples of the use of the function.

Regards,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Chris Linton
> Sent: Monday, October 23, 2006 4:16 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Help with "recode" and "factor" functions
> 
> I have a data set with seven inputs.  Four of which are 
> categorical.  For my midterm, my professor wants us to scale 
> all the inputs.  This means, I pressume, that I have to use 
> 'recode' or 'factor' to transform the categorical data in 
> numerical.  For example, one input variable is 
> 'race=(b,w,h,o)'.  I just want to assign a numerical value to 
> all 'b,w,h,o'.  I thought 'recode' should do this, but it 
> doesn't work.  Here's the code I'm using for recode:
> 
> recode(race, "b='1';w='2';h='3';o='4'")
> 
> this is the error I get:
> Error in eval(expr, envir, enclos) : object "o" not found
> 
> 
> It's not that there's no "o".  If I change the order or 
> combination of the variables, it always can't find one of them.
> 
> I could also use 'factor', from what I hear.  But, I looked 
> at the help section on this function and I ended up more confused.
> 
> 
> How do I code it so these variables take on numerial values?  
> I need to be able to use:
> 
> race.centered = race - mean(race)
> 
> 
> This scaling code doesn't really make sense if the values of 
> 'race' are non-numerical.  I might end up dividing by 2 SD's 
> as well.  But, I don't know if I need to.  I'll have to do 
> some more reading.
> 
> 
> Thank you for your help!
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.



More information about the R-help mailing list