Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 202041

Replace substring if contained in larger string in R

$
0
0

I have a data frame with a column that contains SQL queries. I have a second data frame that has Old Variables to search for in the SQL query and two possible New Variables to replace with. If the Old Variable is contained within the Select...From portion of the SQL query, I need to replace with New Variable 1, if it's outside the Select...From portion, I need to replace with New Variable 2. There are often multiple select statements in a single query. I need to be able to search for and use both options within the same query.

I've been struggling with finding a solution using str_locate_all, and str_replace_all. I've tried finding ways to subset my SQL code and then paste back together, but am at a loss. Below is some test code I've been using.

To be clear, I'm trying to use R to transform SQL code all in one shot.

a <- "2 green Apples"
b <- "10 bananas Select. pnn.green From, and pears and select alskjd.words ( * k from . sup;lwe"
c <- "Select From and 2 green Bananas select from"
d <- "12 yellow Bananas"

fruitquery <- (c(a,b,c,d))
fruitquery<-str_to_lower(fruitquery)
fruitquery

Old_New <- data.frame("OldVariable" = c("green", "bananas", "and", "purple"), 
                         "NewVariable1" = c("Exp_green as green", "Exp_bananas as bananas", "HLM_and as and", "SEM_purple as purple"), 
                         "NewVariable2" = c("Exp_green", "Exp_bananas", "HLM_and", "SEM_purple"))



SelectStatements<-str_extract_all(fruitquery, "select.*?from")
SelectStatements

#Expected output is:
a <- "2 green Apples"
b <- "10 Exp_bananas Select. pnn.Exp_green as green From, Exp_and pears Exp_and select alskjd.words ( * k from . sup;lwe"
c <- "Select From Exp_and 2 Exp_green Exp_Bananas select from"
d <- "12 yellow Exp_Bananas"

Viewing all articles
Browse latest Browse all 202041

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>