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]]
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
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.