简体   繁体   English

如何将特定单词替换为 R 中的另一个单词

[英]how to replace a specific word into another word in R

Im looking for the code in gsub to replace a specific word into xx like this.我在 gsub 中寻找代码以将特定单词替换为 xx 像这样。

x <- c("replace #bitcoin and not #bitoinusdha", "replace #btc and not #btcasdasd")

View(x)
[1] replace  xx and not #bitcoinusdha
[2] replace xx and not #btcasdasd

Hope that someone can help me out.希望有人可以帮助我。

Regards,问候,

Otto奥托

Presuming that the phrases to replace always start with #abc... , then use use a word boundary at the end of each pattern and search for either value.假设要替换的短语总是以#abc...开头,然后在每个模式的末尾使用单词边界并搜索任一值。

x <- c("replace #bitcoin and not #bitcoinusdha", "replace #btc and not #btcasdasd")
gsub("#bitcoin\\b|#btc\\b", "xx", x)
#> [1] "replace xx and not #bitcoinusdha" "replace xx and not #btcasdasd"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM