r - 数据框 R 中的 Gsub 撇号

标签 r dataframe gsub apostrophe

我需要从数据框中删除所有撇号,但一旦我使用......

textDataL <- gsub("'","",textDataL)

当我只想从其中可能存在的任何文本中删除任何撇号时,数据框被破坏并且新数据框仅包含值和 NA?我是否遗漏了一些明显的撇号和数据框?

最佳答案

保持结构完整:

 dat1 <- data.frame(Col1= c("a woman's hat", "the boss's wife", "Mrs. Chang's house", "Mr Cool"),
 Col2= c("the class's hours", "Mr. Jones' golf clubs", "the canvas's size", "Texas' weather"),
 stringsAsFactors=F)

我会用

     dat1[] <- lapply(dat1, gsub, pattern="'", replacement="")

     library(stringr)
     dat1[] <- lapply(dat1, str_replace_all, "'","")
 dat1
#                Col1                 Col2
# 1      a womans hat     the classs hours
# 2    the bosss wife Mr. Jones golf clubs
# 3 Mrs. Changs house     the canvass size
# 4           Mr Cool        Texas weather

关于r - 数据框 R 中的 Gsub 撇号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24576075/

相关文章:

R:是否有一种简单有效的方法来取回 block 对角矩阵的构建 block 矩阵列表?

读取以空格分隔的文本文件,其中第一列也有空格

python - Pandas python根据预先存在的值和条件更新表中的值

r - 使用gsub删除第一个数字字符之前的所有字符串

r - 匹配和替换文本向量中的多个字符串,而不在 R 中循环

regex - R中的gsub和regex遇到问题

r - ggplot2 grouped boxplot 不会为不同的时间点分开组

r - 结合使用R和Google Analytics(分析)

python - 使用键参数按多列对 Pandas 数据框进行排序

python - 将数据帧作为压缩的 csv 直接上传到 s3,而不将其保存在本地计算机上