I am not sure if this is possible.
Right now I am running this using the sqldf package:
Col1 <- c('emdabcer','deffghiee','lmnop')
Col2 <- c(1,2,3)
df <- data.frame(Col1, Col2)
df
Col1 Col2
emdabcer 1
deffghiee 2
lmnop 3
Right now, I am typing in the SQL scripts manually.
sqldf("SELECT *, CASE
WHEN [Col1] LIKE '%abc%' THEN REPLACE([Col1], [Col1], 'Label1')
WHEN [Col1] LIKE '%def%' AND [Col1] LIKE '%ghi%' THEN REPLACE([Col1], [Col1], 'Label2')
ELSE NULL END [Category Label] FROM df")
I have 40 different CASE WHEN instances in my actual dataset.
Is there a way I can use a separate table/dataframe that has a column of my SQL queries and run each row to get my output?
Below is an example dataframe with my queries:
Queries <- c("WHEN [Col1] LIKE '%abc%' THEN REPLACE([Col1], [Col1], 'Label1')",
"WHEN [Col1] LIKE '%def%' AND [Col1] LIKE '%ghi%' THEN REPLACE([Col1], [Col1], 'Label2')",
"WHEN [Col1] LIKE '%mn%' THEN REPLACE([Col1], [Col1], 'Label3')")
Query_df <- data.frame(Queries)
Query_df
Queries
WHEN [Col1] LIKE '%abc%' THEN REPLACE([Col1], [Col1], 'Label1')
WHEN [Col1] LIKE '%def%' AND [Col1] LIKE '%ghi%' THEN REPLACE([Col1], [Col1], 'Label2')
WHEN [Col1] LIKE '%mn%' THEN REPLACE([Col1], [Col1], 'Label3')
And then I would do something like this:
sqldf("SELECT *, CASE
WHILE length(Queries_df) <= length(Queries_df)
BEGIN RUN Queries
END
I know the above is wrong but something along those lines.
Any help would be great thanks!
This is the reference I am looking into: https://www.essentialsql.com/using-while-statement-stored-procedures/