如何使用 purrr::map()
返回示例的 for 循环的结果:
vct_string <- c("old ccar","new carr", "house", "oold house")
df_correction <- data.frame(
pattern = c("ccar", "carr", "oold"),
replacement = c("car", "car", "old"),
stringsAsFactors = FALSE
)
for(i in 1:nrow(df_correction)){
vct_string <- pmap(df_correction, gsub, x = vct_string)[[i]]
}
> vct_string
[1] "old car" "new car" "house" "old house"
最佳答案
实际上,您不需要任何 ReduceMap
函数。只需使用 str_replace_all
它的矢量化
library(stringr)
str_replace_all(vct_string, set_names(df_correction$replacement, df_correction$pattern))
[1] "old car" "new car" "house" "old house"
关于r - 使用 purrr::map 在 for 循环中重写向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68760216/