为什么 gsub 不能按预期工作

问题描述:

我有这个字符串:

c <- "thethirsty thirsty itthirsty (thirsty) is"

我希望输出为

     "thethirsty thirsty itthirsty no is"

这就是我正在尝试的.

gsub(" (thirsty) ", " no ", c)

这就是我得到的.为什么它不起作用?并提出替代方案.

This is what I am getting. Why does not it work? And suggest an alternative to do this.

"thethirsty no itthirsty (thirsty) is" 

默认情况下 gsub 将第一个参数解释为正则表达式.你不想要那个,应该设置 fixed=TRUE:

By default gsub interprets the first parameter as a regular expression. You don't want that and should set fixed=TRUE:

gsub(" (thirsty) ", " no ", c, fixed=TRUE)
#[1] "thethirsty thirsty itthirsty no is"