I am trying to figure out how to use a statement similar to %LIKE% in the R programming language. Using the following stackoverflow post: How to join (merge) data frames (inner, outer, left, right), I was able to figure out how to run basic merges in SQL. But for some reason, this does not work with the %LIKE% condition.
For instance, if I create the following data:
table_a <- data.frame (
"name" = c("John", "ALex", "ToM", "Kev", "Peter"),
"color" = c("red", "blue", "green", "yellow", "pink")
)
name$table_a = as.factor(name$table_a)
table_b <- data.frame (
"name" = c("Johnathan", "Alexander", "Tomas", "Kevin", "Luke", "Ryan"),
"food" = c("pizza", tacos", "sushi", "cake", "brownies", "burgers")
)
name$table_b = as.factor(name$table_b)
table_c <- data.frame (
"name" = c("Johnatha", "Alexande1", "Toma1", "Kevi1", "Luk1"),
"food" = c("pizza", tacos", "sushi", "cake", "brownies")
)
name$table_c = as.factor(name$table_c)
Now what I want to do, is run a "left join" if the name in table_a is contained somewhere within the name in table_b. (using the same logic, it should also be possible to use the one-sided %LIKE as well?)
#Left joins
join_1 = merge(x = table_a, y = table_b, by = "%name%", all.x = TRUE)
join_2 = merge(x = table_b, y = table_c, by = "%name", all.x = TRUE)
In regular SQL statements, it is generally straightforward to select rows of data if they meet a condition specified by %LIKE%. Is the same thing possible in R?
# select using %LIKE% (is there a way to override "case sensitivity" ? e.g. %like% "jOn"?)
selected_1 = table_a[name %like% "Jon"|| "Ale" || "Pet"]
selected_2 = table_a[name %like% "Jon"|| "Ale" || "Pet" || color %like% "ye"]
Thanks