I am trying to perform find and replace function. Prior sleuthing indicates this should be possible with stringr::str_replace and stringr::str_replace_all. However, I'm running into issues because partial matches in longer strings lead to unwanted results. I want the pattern argument to match the whole string not just portions of strings; when it is just a portion I want the replacement operation to ignore the cell. I would greatly appreciate any insight on how to accomplish exact match find and replace operations preferably in a mutate statement.
library(tidyverse)
df <- tribble(
~col1,
"foo",
"foo bar",
"foo",
"foo bar",
"pizza"
)
Then to find and replace, what I see is commonly the following:
df %>%
mutate(col1 = str_replace_all(col1, pattern = "foo", replacement = "foo bar"))
Unfortunately, this produces unwanted results:
foo bar
foo bar bar
foo bar
foo bar bar
pizza
I'm looking to get:
foo bar
foo bar
foo bar
foo bar
pizza
Thanks in advance.