1

Using tidyverse I would like to remove the special characters from "Education" column so that it would just say Masters or Bachelors. Since I'm using Tidyverse I would like to exemplify using piping and keeping the data frame as is:

library(tidyverse)
education <- data.frame(Education = c("Master’s ","Professional ","Bachelor’s"))
education <- sapply(education,str_replace(education,"’",""))
Angel Cloudwalker
  • 2,015
  • 5
  • 32
  • 54
  • Does this answer your question? [Remove all special characters from a string in R?](https://stackoverflow.com/questions/10294284/remove-all-special-characters-from-a-string-in-r) – camille Mar 19 '20 at 14:27

2 Answers2

1

That's what regular expressions are for:

gsub("[^A-Za-z]", "", c("Master’s ","Professional ","Bachelor’s"))

produces:

[1] "Masters"      "Professional" "Bachelors"   
Igor F.
  • 2,649
  • 2
  • 31
  • 39
  • How do I leverage gsub so that I still get back the dataframe in the same schema but with the changes? The above brings back a vector with "V1" column – Angel Cloudwalker Mar 19 '20 at 14:32
  • `gsub` takes a vector and returns a vector. You simply assign that returned vector to `Education`, as you did with the original vector. – Igor F. Mar 19 '20 at 14:49
1

with dplyr

data.frame(Education = c("Master’s ","Professional ","Bachelor’s")) %>% 
   mutate(Education = str_replace(Education,"’",""))
      Education
1      Masters 
2 Professional 
3     Bachelors
jyjek
  • 2,627
  • 11
  • 23