I'm trying to replace exact strings in a column using stringr functions.
The dataset I try it on is this:
data <- data.frame(
column = c("Value", "Values", "Value", "Values")
)
data
column
1 Value
2 Values
3 Value
4 Values
I want to replace "Value" with "Values". I tried str_replace(data$column, "Value", "Values"), but this creates the following unwanted replacements:
[1] "Values" "Valuess" "Values" "Valuess"
I'd like the output to be:
[1] "Values" "Values" "Values" "Values"
Here are a few possibilities using regular expressions:
x <- c("value", "values")
str_replace(x, "value$", "values") #method 1
str_replace(x, "value\\b", "values") #method 2
str_replace(x, "value(?!s)", "values") #method 3
all of the above return the same
[1] "values" "values"
A short explanation: the first method looks for 'value' at the end of a string. The symbol $ matches the end of the string.
The second method looks for 'value' followed by a word boundary.
The third method looks for 'value' followed by anything but the symbol 's'.
You can find a helpful cheat sheet about stringr and regular expressions here. Hope this helps.
Just a simple string comparison should do the trick.
data[data$col == "Value","col"] = "Values"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With