假设我的字符串中有某种模式,该模式出现了已知次数(
n
),并且我们不想对字符串的其余部分(特别是这些模式之间的字符串)做出任何假设。
此外,我有一个长度为
n
(例如,sf
)的向量,我想用相应的元素修改模式的每次出现。因此,对于每场比赛,我想知道比赛已经发生了多少次?
我可以想到以下解决方案:
library(stringr)
sf <- letters[4:1]
ss <- "fdskjhf xx sd ss xx wwwe xx ss xx sdsd"
# ^^ 1st ^^ 2nd ^^ 3rd ^^ 4th
# add: _sf[1] _sf[2] _sf[3] _sf[4]
# that is: xx_d xx_c xx_b xx_a
## add _sf[i] to the ith occurence of "xx" in ss
goal <- "fdskjhf xx_d sd ss xx_c wwwe xx_b ss xx_a sdsd"
my_replacer_factory <- function(sf, start = 0) {
cnt <- start
function(el) {
cnt <<- cnt + 1
paste0(el, "_", rev(sf)[cnt])
}
}
my_replacer <- my_replacer_factory(sf)
(res <- str_replace_all(ss, fixed("xx"), my_replacer))
# [1] "fdskjhf xx_d sd ss xx_c wwwe xx_b ss xx_a sdsd"
all.equal(res, goal)
# [1] TRUE
这显然有效,但感觉容易出错,因为我依赖于
str_replace_all
从右侧开始替换的事实。如果在未来的实现中这种行为发生变化或并行化怎么办?
知道如何以不同的方式实现这一目标吗?理想情况下使用 stringr 函数吗?
类似的想法:
my_replacer_factory <- function(sf) {
suffixes <- rev(sf)
function(el) {
on.exit(suffixes <<- suffixes[-1L], add = TRUE)
paste0(el, "_", suffixes[1L])
}
}
gsubfn
包让这里的事情变得简单
# install.packages("gsubfn")
library(gsubfn)
p <- proto(fun = function(this, x) paste0(x, "_", count))
gsubfn("xx", p, ss)
[1] "fdskjhf xx_1 sd ss xx_2 wwwe xx_3 ss xx_4 sdsd"