r - 如何将特定单词替换为R中的另一个单词
问题描述
我正在寻找 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
希望有人可以帮助我。
问候,
奥托
解决方案
假设要替换的短语始终以 开头#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"
推荐阅读
- arduino - Arduino Uno R3 上的意外编码错误
- html - 无法验证具有“必需”属性的输入
- python - 用于表格数据的自动编码器的高 mse 损失
- node.js - 通过 Travis CI API 传递 Node.JS 运行参数
- javascript - 带有 Next.js 的自定义脚本
- c++ - 完美的转发和dll导出
- angular6 - Valor Software - ngx bootstrap Alert 组件在 Angular 6 中以编程方式具有关闭按钮?
- c# - 如何使用 C# 显示 XML 的层次结构
- php - 循环遍历数组相关模型,创建一个嵌套数组
- sql-server - 使用 SSIS 远程连接到 SQL Server