为什么 gsub 不能按预期工作 [英] Why does gsub not work as expected

查看:54
本文介绍了为什么 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"

这篇关于为什么 gsub 不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆