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")