-1

I'm attempting to replace American style letter grades with the appropriate grade point. For example, A becomes 4.0, A- becomes 3.7, B+ becomes 3.3, etc.

I can't seem to get the regular expressions right using sub to make the changes. I'm having trouble getting it to recognize for example that B, B+, and B- are all separate things, probably because I can't get it make the regular expressions right.

1 Answers1

0

How about creating a lookup table ?

Assuming that you would not be having much grades, you can create your own lookup table like

lookup <- read.table(text = "Grade GradePoint
                                A  4.0
                                A- 3.7
                                B+ 3.3
                                B  3.0
                                B- 2.7", header = T)

Creating some sample data

grades <- read.table(text = "Grade
                               A
                               B+
                               B-", header = T)

and now you can use merge to replace the grades with points.

merge(lookup, grades, by = "Grade")[2]

#    GradePoint
#1        4.0
#2        2.7
#3        3.3

You can also use match with same effect

lookup[match(grades$Grade, lookup$Grade), ][2]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213