Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在两种模式之间替换文本的多个眼点_R - Fatal编程技术网

R 在两种模式之间替换文本的多个眼点

R 在两种模式之间替换文本的多个眼点,r,R,我有一个data.frame,其中有一列包含客户数字路径,如下所示。在每一行中,我想用单词reference替换>和_reference之间的所有文本 例如下面的3行 bing_cpc>uswitch.com_referral bing_cpc>money.co.uk_referral bing_cpc>moneysupermarket.com_referral>google_organic>moneysupermarket.com_referral>goog

我有一个data.frame,其中有一列包含客户数字路径,如下所示。在每一行中,我想用单词reference替换>和_reference之间的所有文本

例如下面的3行

bing_cpc>uswitch.com_referral
bing_cpc>money.co.uk_referral
bing_cpc>moneysupermarket.com_referral>google_organic>moneysupermarket.com_referral>google_cpc>google_cpc
应该是

bing_cpc>Referral
bing_cpc>Referral
bing_cpc>Referral>google_organic>Referral>google_cpc>google_cpc
有什么想法吗? 谢谢

尝试一下:

df$col <- gsub(">.*referral", ">Referral", df$col)

你的问题比看起来更棘手,所以它值得一个详细的答案。首先,让我们将您的示例放在一个向量中:

exStrg <- c(
  'bing_cpc>uswitch.com_referral',
  'bing_cpc>money.co.uk_referral',
  'bing_cpc>moneysupermarket.com_referral>google_organic>moneysupermarket.com_referral>google_cpc>google_cpc'
)
表达式将接受介于第一个“>”和最后一个“\u引用”之间的任何内容。你能用吗?使通配符变懒;这将标识模式的多个实例,但仍将包含中间的所有内容:

> gsub('>.*?_referral', '>Referral', exStrg)
[1] "bing_cpc>Referral"                               
[2] "bing_cpc>Referral"                               
[3] "bing_cpc>Referral>Referral>google_cpc>google_cpc"
相反,您需要将任何后续'>'指示为否定字符:

> gsub('>[^>]*_referral', '>Referral', exStrg)
[1] "bing_cpc>Referral"                                              
[2] "bing_cpc>Referral"                                              
[3] "bing_cpc>Referral>google_organic>Referral>google_cpc>google_cpc"

欢迎来到SO!请阅读并根据它编辑您的问题。为了帮助您,我们需要您使用dput函数发布的数据示例和想要的结果示例。您尝试了什么吗?你到底在哪里被卡住了?当你用谷歌搜索r字符串替换时,你肯定找到了一些有用的资源。@cheikh;让你了解那些反对票。每个人都在这里帮助你,但请记住,你所在的社区主要由忙碌的专业人士组成,他们要求他们花时间和精力解决你的问题。除了参与社区并做同样的事情外,最好的回报方式是。这不仅有利于整个网站,也有助于你:解决一个好问题通常会让你找到一个可能的解决方案。非常感谢。成功了!大家好,英国| BP |品牌|产品| e | def>不可用>英国| B |品牌|纯其他| e | def>英国| NB |纯仅| e | def Hi Carlos,在下文中,我将如何用>BrandUK | BP | Brand | Products(u e|def>unavailable>UK | B | Brand | u Pure Other | e | def>UK | B | U Brand | u Pure Only | e | def>UK | NB
> gsub('>[^>]*_referral', '>Referral', exStrg)
[1] "bing_cpc>Referral"                                              
[2] "bing_cpc>Referral"                                              
[3] "bing_cpc>Referral>google_organic>Referral>google_cpc>google_cpc"