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

From: Petr Pikal <petr.pikal_at_precheza.cz>
Date: Tue 24 Oct 2006 - 06:46:48 GMT


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@gmail.com>
To:             	r-help@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@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@precheza.cz



R-help@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. Received on Tue Oct 24 19:42:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 24 Oct 2006 - 10:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.