I have a dataset of students' report card marks that range from D- to A+. I'd like to recode them into scale of 1-12 (i.e. D- = 1, D = 2 ... A = 11, A+ = 12). Right now I'm suing the revalue
function in plyr
. I have several columns that I'd like to recode - is there a shorter way to do this than running revalue
on each column?
Some data:
student <- c("StudentA","StudentB","StudentC","StudentD","StudentE","StudentF","StudentG","StudentH","StudentI","StudentJ")
read <- c("A", "A+", "B", "B-", "A", "C", "C+", "D", "C", "B+")
write <- c("A-", "B", "C", "C+", "D", "B-", "B", "C", "D+", "B")
math <- c("C", "C", "D+", "A", "A+", "B", "B-", "C-", "D+", "C")
df <- data.frame (student, read, write, math)
Right now I'm recoding them like this:
df$read.r <- as.numeric (revalue (df$read, c("D-" = "1",
"D" = "2",
"D+" = "3",
"C-" = "4",
"C" = "5",
"C+" = "6",
"B-" = "7",
"B" = "8",
"B+" = "9",
"A-" = "10",
"A" = "11",
"A+" = "12"
)))
Instead of running this 3 times (or more) is there a better way? All of the columns have identical values.