lundi 27 janvier 2020

Replace substring if contained in larger string in R

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.

Edit: updated code for readability

a <- "Select 2 green From Apples where bananas=3"
b <- "10 bananas Select small.pears From( select purple.grapes, apples, pears from small s)"
c <- "As Fruit (Select starfruit From fancy orderby green Bananas where bananas=2) With Fruit(select fancy.starfruit from fancy)"
d <- "12 yellow Bananas"

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

Old_New <- data.frame("OldVariable" = c("grapes", "bananas", "apples", "pears"), 
                         "NewVariable1" = c("Exp_grapes as grapes", "Exp_bananas as bananas", "HLM_apples as apples", "SEM_largepears as largepears"), 
                         "NewVariable2" = c("Exp_grapes", "Exp_bananas", "HLM_apples", "SEM_largepears"), stringsAsFactors = FALSE)



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


#Expected output
a <- "Select 2 green From HLM_apples where Exp_bananas=3"
b <- "10 Exp_bananas Select small.SEM_largepears as largepears From( select purple.Exp_grapes as grapes,  HLM_apples as apples, SEM_largepears as largepears from starfruit.small s)"
c <- "As Fruit (Select starfruit From fancy orderby green Exp_bananas where Exp_bananas=2) With Fruit(select fancy.starfruit from fancy)"
d <- "12 yellow Exp_bananas"

Aucun commentaire:

Enregistrer un commentaire