Let's say I have a long character string: pneumonoultramicroscopicsilicovolcanoconiosis. I'd like to use stringr::str_replace_all
to replace certain letters with others. According to the documentation, str_replace_all
can take a named vector and replaces the name with the value. That works fine for 1 replacement, but for multiple it seems to do it iteratively, so the result is a replacement of the prelast iteration. I'm not sure this is the intended behaviour.
library(tidyverse)
text_string = "developer"
text_string %>%
str_replace_all(c(e ="X")) #this works fine
[1] "dXvXlopXr"
text_string %>%
str_replace_all(c(e ="p", p = "e")) #not intended behaviour
[1] "develoeer"
Desired result:
[1] "dpvploepr"
Which I get by introducing a new character:
text_string %>%
str_replace_all(c(e ="X", p = "e", X = "p"))
It's a usable workaround but hardly generalisable. Is this a bug or are my expectations wrong?
I'd like to also be able to replace n letters with n other letters simultaneously, preferably using either two vectors (like "old" and "new") or a named vector as input.
reprex edited for easier human reading