# Install the car package
install.packages("car")
# Load the car package
library(car)
Now recode Grade from 5 to 6:
# Recode grade 5 to grade 6
SchoolData$Grade<-recode(SchoolData$Grade,"5=6")
If you want to recode based on text, use the ‘ mark around the text.
Now recode Grade from 5 to 6:
# Recode grade 5 to grade 6
SchoolData$Grade<-recode(SchoolData$Grade,"'Grade Five'=5")
To set recode multiple values use c()
# Recode grade 5 to grade 6
SchoolData$Grade<-recode(SchoolData$Grade,"c(1,2,3,4,5)='Five or Less'")
Recode can recode data into a new field. This code creates a new field called NewGrade based on Grade. Note that if you don’t specify that value is recoded R will just copy the existing value into the new field.
# Create a new field called NewGrade
SchoolData$NewGrade <- recode(SchoolData$Grade,"5='Elementary'")
Of course, you can convert a value to NA, or NA to a value.
# Recode grade 3 to NA
SchoolData$Grade<-recode(SchoolData$Grade,"3=NA")
# Or recode NA to 7
SchoolData$Grade <- recode(SchoolData$Grade,"NA=7")
One advantage to recode is that it can recode multiple values in one line of code.
# Recode grade 5 to grade 6 and grade 6 to grade 7
SchoolData$Grade<-recode(SchoolData$Grade,"5=6;6=7")