简体   繁体   中英

R How to remove special characters ’ from a data frame column?

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,"’",""))

That's what regular expressions are for:

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

produces:

[1] "Masters"      "Professional" "Bachelors"   

with dplyr

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM