替换多个字符串中的多个单词
Replace multiple words in multiple strings
我想根据另一个数据框中的原始词和替换词替换向量中的词。例如:
要更改的字符串向量:
my_words <- c("example r", "example River", "example R", "anthoer river",
"now a creek", "and another Ck", "example river tributary")
待替换词的数据框和对应的替换词:
my_replace <- data.frame(
original = c("r", "River", "R", "river", "Ck", "creek", "Creek"),
replacement = c("R", "R", "R", 'R', "C", "C", "C"))
我想用向量 my_words
中 my_replace$replacement
中的相应值替换 my_replace$original
中出现的任何一个单词。我尝试使用 stringr::str_replace_all()
,但它替换了 letter/word 的所有实例,而不仅仅是整个单词(例如“another”变成了“anotheR”),这是不受欢迎的。
我想做的伪代码:
str_replace_all(my_words, my_replace$original, my_replace$replacement)
期望的输出:
"example R", "example R", "example R", "another R", "now a C", "and another C", "example R tributary"
我确实找到了使用 for
循环的解决方案,但考虑到我的数据集很大,for
循环选项太慢了。非常感谢任何建议。
这是一种 sub
方法,它只进行一次替换:
my_words <- c("example r", "example River", "example R", "anthoer river",
"now a creek", "and another Ck", "example river tributary")
output <- gsub("\b([rR])(?:iver)?\b|\b([cC])(?:ree)?k\b", "\U\1\U\2", my_words, perl=TRUE)
output
[1] "example R" "example R" "example R"
[4] "anthoer R" "now a C" "and another C"
[7] "example R tributary"
由于所有河流和小溪的替换分别只是 R
和 C
,我们可以捕获每个可能匹配项的第一个字母,然后使用这些字母的大写版本替换.
您需要从 my_words$original
中的单词构建一个基于动态单词边界的模式,然后使用 stringr::str_replace_all
替换为相应的值。请注意 original
短语需要按长度降序排列,以使较长的字符串首先匹配:
my_words <- c("example r", "example River", "example R", "anthoer river", "now a creek", "and another Ck", "example river tributary")
my_replace <- data.frame(original = c("r", "River", "R", "river", "Ck", "creek", "Creek"), replacement = c("R", "R", "R", 'R', "C", "C", "C"))
sort.by.length.desc <- function (v) v[order( -nchar(v)) ]
library(stringr)
regex <- paste0("\b(",paste(sort.by.length.desc(my_replace$original), collapse="|"), ")\b")
str_replace_all(my_words, regex, function(word) my_replace$replacement[my_replace$original==word][[1]][1])
输出:
[1] "example R" "example R" "example R" "anthoer R" "now a C" "and another C" "example R tributary"
正则表达式将是 \b(River|river|creek|Creek|Ck|r|R)\b
,它匹配整个单词中的任何单词。
library(stringi)
stri_replace_all_regex(my_words, "\b" %s+% my_replace$original %s+% "\b", my_replace$replacement, vectorize_all = FALSE)
[1] "example R" "example R" "example R" "anthoer R" "now a C" "and another C" "example R tributary"
我想根据另一个数据框中的原始词和替换词替换向量中的词。例如:
要更改的字符串向量:
my_words <- c("example r", "example River", "example R", "anthoer river",
"now a creek", "and another Ck", "example river tributary")
待替换词的数据框和对应的替换词:
my_replace <- data.frame(
original = c("r", "River", "R", "river", "Ck", "creek", "Creek"),
replacement = c("R", "R", "R", 'R', "C", "C", "C"))
我想用向量 my_words
中 my_replace$replacement
中的相应值替换 my_replace$original
中出现的任何一个单词。我尝试使用 stringr::str_replace_all()
,但它替换了 letter/word 的所有实例,而不仅仅是整个单词(例如“another”变成了“anotheR”),这是不受欢迎的。
我想做的伪代码:
str_replace_all(my_words, my_replace$original, my_replace$replacement)
期望的输出:
"example R", "example R", "example R", "another R", "now a C", "and another C", "example R tributary"
我确实找到了使用 for
循环的解决方案,但考虑到我的数据集很大,for
循环选项太慢了。非常感谢任何建议。
这是一种 sub
方法,它只进行一次替换:
my_words <- c("example r", "example River", "example R", "anthoer river",
"now a creek", "and another Ck", "example river tributary")
output <- gsub("\b([rR])(?:iver)?\b|\b([cC])(?:ree)?k\b", "\U\1\U\2", my_words, perl=TRUE)
output
[1] "example R" "example R" "example R"
[4] "anthoer R" "now a C" "and another C"
[7] "example R tributary"
由于所有河流和小溪的替换分别只是 R
和 C
,我们可以捕获每个可能匹配项的第一个字母,然后使用这些字母的大写版本替换.
您需要从 my_words$original
中的单词构建一个基于动态单词边界的模式,然后使用 stringr::str_replace_all
替换为相应的值。请注意 original
短语需要按长度降序排列,以使较长的字符串首先匹配:
my_words <- c("example r", "example River", "example R", "anthoer river", "now a creek", "and another Ck", "example river tributary")
my_replace <- data.frame(original = c("r", "River", "R", "river", "Ck", "creek", "Creek"), replacement = c("R", "R", "R", 'R', "C", "C", "C"))
sort.by.length.desc <- function (v) v[order( -nchar(v)) ]
library(stringr)
regex <- paste0("\b(",paste(sort.by.length.desc(my_replace$original), collapse="|"), ")\b")
str_replace_all(my_words, regex, function(word) my_replace$replacement[my_replace$original==word][[1]][1])
输出:
[1] "example R" "example R" "example R" "anthoer R" "now a C" "and another C" "example R tributary"
正则表达式将是 \b(River|river|creek|Creek|Ck|r|R)\b
,它匹配整个单词中的任何单词。
library(stringi)
stri_replace_all_regex(my_words, "\b" %s+% my_replace$original %s+% "\b", my_replace$replacement, vectorize_all = FALSE)
[1] "example R" "example R" "example R" "anthoer R" "now a C" "and another C" "example R tributary"