Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace an exact string using stringr functions?

Tags:

regex

r

stringr

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"
like image 702
MetaPhilosopher Avatar asked Jun 21 '26 23:06

MetaPhilosopher


2 Answers

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.

like image 180
Cettt Avatar answered Jun 23 '26 14:06

Cettt


Just a simple string comparison should do the trick.

data[data$col == "Value","col"] = "Values"
like image 38
MSW Data Avatar answered Jun 23 '26 16:06

MSW Data



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!