Sam Gilbert
Sam Gilbert

Reputation: 1702

Changing a pattern that occurs multiple times in a string in R

I have a dataframe with one column, where each row represents part of a sql select statement, for example below:

test <-
  bind_rows(
    data.frame(text = "spend_1 + spend_2", stringsAsFactors = FALSE),
    data.frame(text = "spend_1 + spend_2 + spend_3", stringsAsFactors = FALSE),
    data.frame(text = "spend_2 - spend_3", stringsAsFactors = FALSE)
  )

print(test)

Source: local data frame [3 x 1]

                         text
                        (chr)
1           spend_1 + spend_2
2 spend_1 + spend_2 + spend_3
3           spend_2 - spend_3

I would like to, for each instance of \w+, Add the table alias to the variable. For example:

                         text   text_adj

1           spend_1 + spend_2   a.spend_1 + a.spend_2   
2 spend_1 + spend_2 + spend_3   a.spend_1 + a.spend_2 + a.spend_3
3           spend_2 - spend_3   a.spend_2 - a.spend_3

Using str_replace I can replace each variable with "some text", but I can't figure out how I can then replace each instance with the alias + original variable text

library(stringr)

str_replace_all(text, "\\w+", "some text")

Upvotes: 1

Views: 338

Answers (1)

nrussell
nrussell

Reputation: 18602

You just need to capture the pattern and reference it with \\1. For example,

test %>%
    mutate(., text2 = str_replace_all(text, "(\\w+)", "alias.\\1"))
# Source: local data frame [3 x 2]
# 
#                          text                                         text2
#                         (chr)                                         (chr)
# 1           spend_1 + spend_2                 alias.spend_1 + alias.spend_2
# 2 spend_1 + spend_2 + spend_3 alias.spend_1 + alias.spend_2 + alias.spend_3
# 3           spend_2 - spend_3                 alias.spend_2 - alias.spend_3

Upvotes: 2

Related Questions