简体   繁体   English

R 如何从数据框列中删除特殊字符 ’?

[英]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.使用 tidyverse 我想从“教育”列中删除特殊字符,以便它只说硕士或学士。 Since I'm using Tidyverse I would like to exemplify using piping and keeping the data frame as is:由于我使用的是 Tidyverse,我想举例说明使用管道并保持数据框不变:

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使用dplyr

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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