在R中替换符号“ - ”

时间:2017-05-31 04:36:13

标签: r regex

我正在处理需要用空字符串替换字符串– Greek City Times的项目。我试着这样做。

master.DF.new$Title <- enc2utf8(trim(gsub("– Greek City Times", "", master.DF.new$Title)))

但似乎没有接受符号-

2 个答案:

答案 0 :(得分:0)

您应该在短划线周围放置单括号,但在引号内。 参见:

gsub(pattern = "[-]", replacement = "", x = "-Greek City Times")
#[1] "Greek City Times"

但在这种情况下不需要:

gsub(pattern = "- Greek City Times", "", "Is- Greek City Times This It?")
[1] "Is This It?"

如果您给我们一个复制问题的方法,我会进一步编辑。

修改

鉴于你只想删除那些字符串条目,如何:

TitleTable <- c("– Greek City Times", "- Greek City Times", "Not Greek City", "Also Not Greek City")

TitleTable[-grep("Greek City Times", TitleTable)]
[1] "Not Greek City"     
[2] "Also Not Greek City"

答案 1 :(得分:0)

我们可以使用stringi

library(stringi)

master.DF.new$Title<-stri_replace_all(master.DF.new$Title,""– Greek City Times" ,fixed = "" )