First write a function for the replacements
word_correct <- function(string) {
df_correction <- data.frame(
pattern = c("old ccar", " new carr", "oold house", "house"), # changed from OP
replacement = c("car", "car", "old", "house"),
stringsAsFactors = FALSE
)
df_correction[ which(df_correction$pattern == string), "replacement"]
}
# Testing
word_correct("oold")
word_correct("ccar")
Then you can pass that function as an argument in purrr::map
map_chr(vct_string, word_correct) # using map_chr to return a vector instead of a list which is what map returns
Since you are using a mapping table to replace individual words, you can actually use map in a second function to get your desired results.
vct_string <- c("old ccar","new carr", "house", "oold house")
single_word_correct <- function(string) {
df_correction <- data.frame(
pattern = c("ccar", "carr", "oold"),
replacement = c("car", "car", "old"),
stringsAsFactors = FALSE
)
if(string %in% df_correction$pattern){
df_correction[ which(df_correction$pattern == string), "replacement"]
} else {
string
}
}
multi_word_correct <- function(string){
strings <- strsplit(string, " ")[[1]]
paste(map_chr(strings, single_word_correct), collapse = " ")
}
map_chr(vct_string, multi_word_correct)