簡體   English   中英

R 如何從數據框列中刪除特殊字符 ’?

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

使用 tidyverse 我想從“教育”列中刪除特殊字符,以便它只說碩士或學士。 由於我使用的是 Tidyverse,我想舉例說明使用管道並保持數據框不變:

library(tidyverse)
education <- data.frame(Education = c("Master’s ","Professional ","Bachelor’s"))
education <- sapply(education,str_replace(education,"’",""))

這就是正則表達式的用途:

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

產生:

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

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