Here are two solutions for removing all spaces.
String <- c("math course", "english course ")
gsub(" ", "", String)
#> [1] "mathcourse" "englishcourse"
library(stringr)
str_remove_all(String, " ")
#> [1] "mathcourse" "englishcourse"
Created on 2019-08-31 by the reprex package (v0.2.1)