Hi,
I would like to identify multiple patterns in a character vector and replace each pattern with a specific replacement.
For this, I extract each match, modify the letters in the way I need and would like to replace the old matches with the modified ones in the original vector.
words <- c( "apple", "banana", "coconut", "froppyland")
extraction <- str_extract_all(words, "[aeiou]")
workcases <- unlist(extraction)
workcases <- str_c(workcases, "x", workcases)
str_replace_all(words, "[aeiou]", workcases)
Here, I extract all cases where a vowel appears in the vector, I create a replacement vector so that each vowel is transformed into "vowel-x-vowel" and then I would like to replace each match in the words-vector with the transformed cases.
The problem is I get this output:
[1] "axapplaxa" "bexenexenexe" "caxacaxanaxat" "fraxapylaxand" "axapplaxa" "boxonoxonoxo" "coxocoxonoxot" "fruxupyluxund" "oxopploxo"
[10] "baxanaxanaxa"
Warning message:
In stri_replace_all_regex(string, pattern, fix_replacement(replacement), :
longer object length is not a multiple of shorter object length
... which means that the str_replace_all function does not apply each element of the replacement vector to the appropriate pattern match, even though the amount of matches and the lenghth of the replacement vector are identical.
Can someone tell me how I can replace a number of matches with an equal number of replacements?
Thanks in advance