Hi all, I'm trying to use stringr::str_replace_all to substitute a number of fixed patterns in a text vector with one pattern. So something like:
x = c('banana_cake', 'orange_pear', 'lemon_meringue_tart')
pats = c('banana', 'pear', 'lemon')
str_replace_all(x, pats, 'NOOO')
# [1] "NOOO_cake" "orange_NOOO" "NOOO_meringue_tart"
However, if I change the size of either the pattern vector or the string vector to substitute on, I get either errors about vector size or no replacement (or both):
patterns = c('banana', 'apple')
desserts = c('apple_pie', 'banana_cake', 'pumpkin_pie', 'grape_soda', 'something_else')
str_replace_all(desserts, patterns, 'NOPE')
# [1] "apple_pie" "banana_cake" "pumpkin_pie" "grape_soda"
# [5] "something_else"
# Warning message:
# In stri_replace_all_regex(string, pattern, fix_replacement(replacement), :
# longer object length is not a multiple of shorter object length
Am I just trying to use str_replace_all for something it's not really designed to do?
(Note: I chose to put this Q here, rather than SO, because I suspect I am in fact trying to use the wrong tool for the job and that this will probably turn into a discussion on the tool I should be using
)