简体   繁体   中英

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.

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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