キーを使用して、文字列内のテキストを繰り返し置換しようとしています。私は以下のコードを持っていますが、それを行うためのより簡単で効率的な方法があるかどうか疑問に思っています
library(stringi)
library(magrittr)
library(dplyr)
old_texts = c("blah blah value1 blah blah value2",
"blah value1 blah value2")
key = data_frame(
old = c("value1", "value2"),
new = c("value3", "value4") )
replace_key_individual = function(old_text, key)
key %>%
mutate(call = paste0(
"stri_replace_all_fixed('", old, "','", new, "')") ) %>%
summarize(new_call =
call %>%
paste(collapse = " %>% ")) %>%
`$`(new_call) %>%
paste("old_text %>% ", .) %>%
parse(text = .) %>%
eval
replace_key = function(old_texts, key)
old_texts %>%
sapply(. %>% replace_key_individual(key)) %>%
unname
replace_key(old_texts, key = key)