I have a list that I've extracted from a table for the purpose of cleaning up the data and then adding it back as a new clean column. The column originally included country names and codes with some special characters ("*"). So far, I have this code working to remove the codes in parentheses and the special characters (which might not be the easiest way to do it), however the last line isn't removing the spaces:
> dput(head(country.names, 10))
c(" United States (USA)", " China (CHN)", " Japan (JPN)*", " Great Britain (GBR)",
" ROC (ROC)", " Australia (AUS)", " Netherlands (NED)", " France (FRA)",
" Germany (GER)", " Italy (ITA)")
So far, I have this code working to remove the codes in parentheses and the special characters (which might not be the easiest way to do it), however the last line isn't removing the spaces:
> name <- gsub("\\([^\\)]*\\)", "", country.names) %>%
+ gsub("\\*", "", .) %>%
+ gsub("^[[:space:]]+|$[[space:]]+", "", .)
(I also tried gsub("^ | $", "", .) and trimws(name, which = "both") to remove spaces without luck)
This is a sample of the output I have using this code:
[1]" United States " " China " " Japan " " Great Britain " " ROC " " Australia " " Netherlands "
[8] " France " " Germany " " Italy " " Canada " " Brazil " " New Zealand " " Cuba "