[英]Replace Nth occurrence of a word (substring) in a string in R, N is the value of an integer column
[英]Replace 'from nth to the last' occurrence of word in string/text
給定以下字符串:
mystring <- "one fish two fish red fish blue fish"
以下 function 允許替換其中第 n 次出現的單詞:
replacerFn <- function(String, word, rword, n){
stopifnot(n >0)
pat <- sprintf(paste0("^((.*?\\b", word, "\\b.*?){%d})\\b",
word,"\\b"), n-1)
rpat <- paste0("\\1", rword)
if(n >1) {
stringr::str_replace(String, pat, rpat)
} else {
stringr::str_replace(String, word, rword)
}
}
replacerFn(mystring, "fish", "dog", 1)
#[1] "one dog two fish red fish blue fish"
replacerFn(mystring, "fish", "dog", 2)
#[1] "one fish two dog red fish blue fish"
replacerFn(mystring, "fish", "dog", 3)
#[1] "one fish two fish red dog blue fish"
replacerFn(mystring, "fish", "dog", 4)
#[1] "one fish two fish red fish blue dog"
我們如何調整這個 function 來替換第 n 個到最后一個出現的單詞?
倒數第二名:
"one fish two dog red dog blue dog"
倒數第三名:
"one fish two fish red dog blue dog"
等等...?
我嘗試了 str_replace_all 或調整正則表達式部分 {1,} 但沒有成功。
謝謝你的幫助!
這是gsubfn
的一個更簡單的選擇
library(gsubfn)
replacerFn2 <- function(String, word, rword, n) {
p <- proto(fun = function(this, x) if (count >= n) rword else x)
gsubfn(word, p, String)
}
replacerFn2(mystring, "fish", "dog", 2)
#[1] "one fish two dog red dog blue dog"
replacerFn2(mystring, "fish", "dog", 3)
#[1] "one fish two fish red dog blue dog"
replacerFn2(mystring, "fish", "dog", 4)
#[1] "one fish two fish red fish blue dog"
您的 function 相當復雜,有條件。 另一種方法是將字符串拆分為單個單詞的字符向量,對其應用stringr
函數,然后將其連接回單個字符串:
library(stringr)
replace_function <- function(string, word, rword, n) {
vec <- unlist(strsplit(string, " "))
vec[str_which(vec, word)[n]] <- rword
str_c(vec, collapse = " ")
}
replace_function(mystring, "fish", "dog", 1)
[1] "one dog two fish red fish blue fish"
replace_function(mystring, "fish", "dog", 2)
[1] "one fish two dog red fish blue fish"
現在,您可以使用rev()
非常輕松地修改此 function 以替換最后的第 n 個元素:
replace_end_function <- function(string, word, rword, n) {
vec <- unlist(strsplit(string, " "))
vec[rev(str_which(vec, word))[n]] <- rword
str_c(vec, collapse = " ")
}
replace_end_function(mystring, "fish", "dog", 1)
[1] "one fish two fish red fish blue dog"
replace_end_function(mystring, "fish", "dog", 2)
[1] "one fish two fish red dog blue fish"
編輯(我認為“倒數第n個”的意思是“倒數第n個”是我的錯):
要將第 n 個元素替換為最后一個元素:
replace_end_function <- function(string, word, rword, n) {
vec <- unlist(strsplit(string, " "))
vec[str_which(vec, word)[n:length(str_which(vec, word))]] <- rword
str_c(vec, collapse = " ")
}
replace_end_function(mystring, "fish", "dog", 1)
[1] "one dog two dog red dog blue dog"
replace_end_function(mystring, "fish", "dog", 2)
[1] "one fish two dog red dog blue dog"
replace_end_function(mystring, "fish", "dog", 3)
[1] "one fish two fish red dog blue dog"
replace_end_function(mystring, "fish", "dog", 4)
[1] "one fish two fish red fish blue dog"
您可以在 function replacerFn
中使用grep
,即
replacerFn <- function(String, word, rword, n) {
v <- unlist(strsplit(String,split = " "))
v[grep(word,v)[n:length(v)]] <- rword
return(paste0(v,collapse = " "))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.