I have created an R script to calculate final scores for a class I am teaching, and would like to use R to create the final letter grade. How can I take a vector of percentages such as:
grades <- c(.47, .93, .87, .86, .79, .90)
and convert them to letter grades given the appropriate percentage ranges, say, 70-79 = C, 80-89 = B, 90-100 = A?
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 suggested you can use cut. The right = FALSE options means that the interval is not inclusive on the right hand end. For cut to work just pass your grades and the cut points you want as the argument.
Thanks to @jlhoward for the improvement suggestion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With