Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add custom text in string based on values specified in text vector in R

Let’s say I have a vector of string values that looks something like:

vec_vals<-c("value1", "value2", "value3")

and a text string which at certain points may contain those values, something like:

text_string<-"value1 blah blah blah value2"

I am trying to add a value, let’s say x. in the text_string in front of any of vec_vals that appear in the text_string, so in this case the desired result would be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

text_string_cleaned<-"x.value1 blah blah blah x.value2"

Thanks!

>Solution :

You could use the stringr package:

library(stringr)
vec_vals <- c("value1", "value2", "value3")
text_string <- "value1 blah blah blah value2"

# Create the pattern "(value1|value2|value3)" which in str_replace_all will detect 
#  all matches of value1, value2, or value3.
pattern <- paste0("(", paste(vec_vals, collapse = "|"), ")")

# Replace all occurrences of value1, value2, or value3 with .xvalue1, .xvalue2, or.xvalue3
str_replace_all(text_string, pattern = pattern, replacement = "x.\\1")

#output
"x.value1 blah blah blah x.value2"

The purpose of \\1 in a regular expression is to insert the exact text that was matched by the first group of characters enclosed in parentheses in the pattern into the replacement string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading