Convert percentage to letter in R

I created an R script to calculate final grades for a class I am teaching and would like to use R to generate the final grade. How can I take a vector of percentages like:

grades <- c(.47, .93, .87, .86, .79, .90)

      

and convert them to graphs of letters given the appropriate percentage ranges, for example 70-79 = C, 80-89 = B, 90-100 = A?

+3


source to share


2 answers


grades <- c(.47, .93, .87, .86, .79, .90)
letter.grade <- cut(grades, c(0, 0.7,0.8,0.9,Inf), right=FALSE, labels=letters[4:1]);
letter.grade

      

As @MrFlick suggests using a cut. The option right = FALSE means that no spacing is included on the right side. For cut to work, just pass your scores and cut points you want as an argument.



Thanks to @jlhoward for suggesting improvement

+3


source


Another option is to use findInterval



LETTERS[4:1][findInterval(grades, c(0,0.7,0.8,0.9))]
#[1] "D" "A" "B" "B" "C" "A"

      

+3


source







All Articles