1

Using R, my data frame capstone3 with column Certificate...HQA has the following levels:

levels(capstone3$Certificate...HQA)

 [1] "CUM LAUDE"                     "DIPLOM"                       
 [3] "DOCTORATE"                     "GRADUATE DIPLOMA"             
 [5] "HIGHEST HONS"                  "HONOURS (DISTINCTION)"        
 [7] "HONOURS (HIGHEST DISTINCTION)" "HONS"                         
 [9] "HONS I"                        "HONS II"                      
[11] "HONS II LOWER"                 "HONS II UPPER"                
[13] "HONS III"                      "HONS UNCLASSIFIED"            
[15] "HONS WITH MERIT"               "MAGNA CUM LAUDE"              
[17] "MASTER'S DEGREE"               "OTHER HONS"                   
[19] "PASS DEGREE"                   "PASS WITH CREDIT"             
[21] "PASS WITH DISTINCTION"         "PASS WITH HIGH MERIT"         
[23] "PASS WITH MERIT"               "SUMMA CUM LAUDE" 

I wrote a code to reduce the number of levels by substituting level [7] with level [9], level [6] with level [12], etc:

capstone3$Certificate...HQA <- as.factor(capstone3$Certificate...HQA)

capstone3$Certificate...HQA <- gsub("HONOURS (HIGHEST DISTINCTION)","HONS I", capstone3$Certificate...HQA)

capstone3$Certificate...HQA <- gsub("HONOURS (DISTINCTION)","HONS II UPPER", capstone3$Certificate...HQA)

capstone3$Certificate...HQA <- gsub("HONS WITH MERIT","HONS II LOWER", capstone3$Certificate...HQA)

But the above gsub code did not replace the names in the column, could someone kindly point out the problem with my code please?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

2

Parentheses () are special characters used in regular expressions to create groups. If you have literal parentheses you need to escape them using \\

gsub("HONOURS \\(HIGHEST DISTINCTION\\)","HONS I", capstone3$Certificate...HQA)

OR as @ManuelBickel: Using fixed = TRUE the pattern is a string will be matched as is.

gsub("HONOURS (HIGHEST DISTINCTION)","HONS I", capstone3$Certificate...HQA, fixed = TRUE)
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • You may also turn on `fixed = TRUE` in `gsub` to make the string be recognized as is without any parsing, e.g., `gsub("HONOURS (HIGHEST DISTINCTION)","HONS I", "HONOURS (HIGHEST DISTINCTION)", fixed = TRUE)` – Manuel Bickel Aug 05 '18 at 11:17
  • 1
    @ManuelBickel many thanks for pointing that out. – A. Suliman Aug 05 '18 at 11:20