I have written a function in R that will convert a data frame containing letter grades into numeric grades. I then use sapply() on each column of the data frame. Is there a
First vectorize your function: you could do this with ifelse
, or:
grvec <- c("A+"=4.3,"A"=4,"A-"=3.7,"B+"=3.3,"B"=3,"B-"=2.7,
"C+"=2.3,"C"=2,"C-"=1.7,"D+"=1.3,"D"=1,"D-"= 0.7,
"F"=0)
grades <- data.frame(final_exam=c("C","C-","D+"),
quiz_avg=c("A","B-","Q"))
## final_exam quiz_avg
## 1 C A
## 2 C- B-
## 3 D+ Q
num_grades <- apply(grades,2,function(x) grvec[as.character(x)])
## final_exam quiz_avg
## [1,] 2.0 4.0
## [2,] 1.7 2.7
## [3,] 1.3 NA
Here is a pretty fast hash approach that shines the more grades you have:
library(qdap)
grvec <- list("A+"=4.3,"A"=4,"A-"=3.7,"B+"=3.3,"B"=3,"B-"=2.7,
"C+"=2.3,"C"=2,"C-"=1.7,"D+"=1.3,"D"=1,"D-"= 0.7, "F"=0)
dat[] <- lapply(dat, lookup, list2df(grvec)[, c(2:1)])
## final_exam quiz_avg homework_avg
## 1 2.0 4.0 4
## 2 1.7 2.7 4
## 3 1.3 3.3 4
## 4 3.3 3.3 4
## 5 0.0 3.3 4
## 6 3.0 3.7 4
I would rewrite your convert_grades
function as follows:
convert_grades <- function(x) {
A <- factor(x, levels=c("A+", "A", "A-",
"B+", "B", "B-",
"C+", "C", "C-",
"D+", "D", "D-", "F"))
values <- c(4.3, 4, 3.7,
3.3, 3, 2.7,
2.3, 2, 1.7,
1.3, 1, 0.7, 0)
values[A]
}
Then, I would do the conversion like this:
num_grades <- grades
num_grades[] <- lapply(num_grades, convert_grades)
num_grades
final_exam quiz_avg homework_avg
1 2.0 4.0 4
2 1.7 2.7 4
3 1.3 3.3 4
4 3.3 3.3 4
5 0.0 3.3 4
6 3.0 3.7 4