gpt4 book ai didi

r - 在字符串替换中避免 for 循环?

转载 作者:行者123 更新时间:2023-12-01 01:11:59 25 4
gpt4 key购买 nike

我有数据、一个字符向量(最终我会把它折叠起来,所以我不在乎它是一个向量还是被视为单个字符串)、一个模式向量和一个替换向量。我希望数据中的每个模式都被其各自的替换所替换。我用 stringr 搞定了和一个 for 循环,但是有没有更像 R 的方法来做到这一点?

require(stringr)
start_string <- sample(letters[1:10], 10)
my_pattern <- c("a", "b", "c", "z")
my_replacement <- c("[this was an a]", "[this was a b]", "[this was a c]", "[no z!]")
str_replace(start_string, pattern = my_pattern, replacement = my_replacement)
# bad lengths, doesn't work

str_replace(paste0(start_string, collapse = ""),
pattern = my_pattern, replacement = my_replacement)
# vector output, not what I want in this case

my_result <- start_string
for (i in 1:length(my_pattern)) {
my_result <- str_replace(my_result,
pattern = my_pattern[i], replacement = my_replacement[i])
}
> my_result
[1] "[this was a c]" "[this was an a]" "e" "g" "h" "[this was a b]"
[7] "d" "j" "f" "i"

# This is what I want, but is there a better way?

就我而言,我知道每个模式最多只会出现一次,但并非每个模式都会出现。我知道我可以使用 str_replace_all如果模式可能出现不止一次;我希望解决方案也能提供该选项。我还想要一个使用 my_pattern 的解决方案和 my_replacement这样它就可以成为以这些向量作为参数的函数的一部分。

最佳答案

我敢打赌还有另一种方法可以做到这一点,但我的第一个想法是 gsubfn :

my_repl <- function(x){
switch(x,a = "[this was an a]",
b = "[this was a b]",
c = "[this was a c]",
z = "[this was a z]")
}

library(gsubfn)
start_string <- sample(letters[1:10], 10)
gsubfn("a|b|c|z",my_repl,x = start_string)

如果您正在为列表元素搜索可接受的有效名称的模式,这也将起作用:
names(my_replacement) <- my_pattern
gsubfn("a|b|c|z",as.list(my_replacement),start_string)

编辑

但坦率地说,如果我真的必须在自己的代码中做很多事情,我可能只会做 for循环的东西,包裹在一个函数中。这是一个使用 sub 的简单版本和 gsub而不是来自 的函数纵梁 :
vsub <- function(pattern,replacement,x,all = TRUE,...){
FUN <- if (all) gsub else sub
for (i in seq_len(min(length(pattern),length(replacement)))){
x <- FUN(pattern = pattern[i],replacement = replacement[i],x,...)
}
x
}

vsub(my_pattern,my_replacement,start_string)

但是,当然,没有众所周知的内置函数的原因之一可能是像这样的顺序替换不可能非常脆弱,因为它们非常依赖于顺序:
vsub(rev(my_pattern),rev(my_replacement),start_string)
[1] "i" "[this w[this was an a]s [this was an a] c]"
[3] "[this was an a]" "g"
[5] "j" "d"
[7] "f" "[this w[this was an a]s [this was an a] b]"
[9] "h" "e"

关于r - 在字符串替换中避免 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com