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

Petr Pikal petr.pikal at precheza.cz
Tue Oct 24 08:46:48 CEST 2006


Hi

I did not find recode function in base R so it is probably in some 
package. However you can use factor and as.numeric e.g.

vec<-sample(c("b","w","h","o"),20, replace=T)
vec
 [1] "h" "w" "w" "o" "h" "b" "b" "w" "o" "h" "o" "h" "w" "w" "b" "b" 
"h" "b" "w" "h"
vec.f<-as.factor(vec)
vec.n<-as.numeric(vec.f)

# if you want specific levels ordering
vec.f.2<-factor(vec, levels=c("b", "w", "h","o"))
vec.n.2<-as.numeric(vec.f.2)

vec.f
 [1] h w w o h b b w o h o h w w b b h b w h
Levels: b h o w
vec.f.2 # notice levels order
 [1] h w w o h b b w o h o h w w b b h b w h
Levels: b w h o

> vec.n
 [1] 2 4 4 3 2 1 1 4 3 2 3 2 4 4 1 1 2 1 4 2
> vec.n.2
 [1] 3 2 2 4 3 1 1 2 4 3 4 3 2 2 1 1 3 1 2 3

HTH
Petr


On 23 Oct 2006 at 17:15, Chris Linton wrote:

Date sent:      	Mon, 23 Oct 2006 17:15:41 -0400
From:           	"Chris Linton" <connect.chris at gmail.com>
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.

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list