Merge two tables in LIKE, but for whole rows are not part of rows

This is my first post / question, so be kind. I have a dataframe like this:

        id                             product
1 00109290                    Wax Salt; Pepper
2 23243242                          Wood Stuff
3 23242433   Magic Unicorn Powder and My Tears
4 23778899                             gelatin
5 25887766                                tin;
6  7786655             fart noises, and things
7  3432422 --spearmint bacon& hydrangia leaves

I have a lookup table as follows:

        ingredients
1               wax
2              salt
3              wood
4          my tears
5    unicorn powder
6           gelatin
7               tin
8  hydrangia leaves
9         spearmint
10            bacon

I want to combine them in whole lines to get the following:

     id                             product      ingredients
1  00109290                    Wax Salt; Pepper              wax
2  00109290                    Wax Salt; Pepper             salt
3  23243242                          Wood Stuff             wood
4  23242433   Magic Unicorn Powder and My Tears         my tears
5  23242433   Magic Unicorn Powder and My Tears   unicorn powder
6  23778899                             gelatin          gelatin
7  25887766                                tin;              tin
8   3432422 --spearmint bacon& hydrangia leaves hydrangia leaves
9   3432422 --spearmint bacon& hydrangia leaves        spearmint
10  3432422 --spearmint bacon& hydrangia leaves            bacon

Instead, I get this (notification line 7 is not needed):

         id                             product      ingredients
1  00109290                    Wax Salt; Pepper              wax
2  00109290                    Wax Salt; Pepper             salt
3  23243242                          Wood Stuff             wood
4  23242433   Magic Unicorn Powder and My Tears         my tears
5  23242433   Magic Unicorn Powder and My Tears   unicorn powder
6  23778899                             gelatin          gelatin
7  23778899                             gelatin              tin
8  25887766                                tin;              tin
9   3432422 --spearmint bacon& hydrangia leaves hydrangia leaves
10  3432422 --spearmint bacon& hydrangia leaves        spearmint
11  3432422 --spearmint bacon& hydrangia leaves            bacon

It hurts so close to me, but I am not right about gelatin with tin. I want to match whole words, not parts of words. I have tried many different methods, this one comes closest:

library(sqldf)
id <- c('00109290', '23243242', '23242433', 
        '23778899', '25887766', '7786655', 
        '3432422')
product <- c('Wax Salt; Pepper', 'Wood Stuff', 
             'Magic Unicorn Powder and My Tears', 
             'gelatin', 'tin;', 'fart noises, and things', 
             '--spearmint bacon& hydrangia leaves')

ingredients <- c('wax', 'salt', 'wood', 'my tears', 
                 'unicorn powder', 'gelatin', 'tin', 
                 'hydrangia leaves', 
                 'spearmint', 'bacon') 

products <- data.frame(id, product)
ingred <- data.frame(ingredients)    
new_df <- sqldf("SELECT * from products 
                 join ingred on product LIKE '%' || ingredients || '%'")

Encourage any advice. Maybe a completely different approach is needed? I also welcome advice on the quality of the matter, this is my first one so that you immediately put me straight.

+4
2

fuzzyjoin str_detect from stringr:

library(fuzzyjoin)
library(stringr)

f <- function(x, y) {
  # tests whether y is an ingredient of x
  str_detect(x, regex(paste0("\\b", y, "\\b"), ignore_case = TRUE))
}

fuzzy_join(products, 
           ingred, 
           by = c("product" = "ingredients"), 
           match_fun = f)
#         id                           product    ingredients
# 1   109290                  Wax Salt; Pepper            wax
# 2   109290                  Wax Salt; Pepper           salt
# 3 23243242                        Wood Stuff           wood
# 4 23242433 Magic Unicorn Powder and My Tears       my tears
# 5 23242433 Magic Unicorn Powder and My Tears unicorn powder
# 6 23778899                           gelatin        gelatin

products <- read.table(text = "
        id                             product
1 00109290                  'Wax Salt; Pepper'
2 23243242                        'Wood Stuff'
3 23242433 'Magic Unicorn Powder and My Tears'
4 23778899                             gelatin                  
  ", stringsAsFactors = FALSE)

ingred <- read.table(text = "
       ingredients
1              wax
2             salt
3             wood
4       'my tears'
5 'unicorn powder'
6          gelatin
7              tin
  ", stringsAsFactors = FALSE)

+2

OR , - / .

new_df <- sqldf("SELECT * from products 
                 join ingred on Replace(product, ';', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, ';', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, ';', '') = ingredients
                ")

UNION . :

new_df <- sqldf("SELECT * from products 
                 join ingred on Replace(product, ';', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, ';', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, ';', '') = ingredients
                 UNION    
                 SELECT * from products 
                 join ingred on Replace(product, '!', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '!', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '!', '') = ingredients
                ")

UNIONs R- SQL:

sql <- paste(lapply(c("!", "#", "$", "%", "(", ")", ":", ";", ".", "?", ">", "<", "/", "\\\\", "|"), 
             function(i)
                paste0("SELECT * from products 
                        join ingred on Replace(product, '", i, "', '') LIKE '% ' || ingredients || '%'
                                    OR Replace(product, '", i, "', '') LIKE '%' || ingredients || ' %'
                                    OR Replace(product, '", i, "', '') = ingredients
                       ")
       ), collapse = "UNION ")

cat(paste(sql))

SELECT * from products 
                   join ingred on Replace(product, '!', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '!', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '!', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '#', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '#', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '#', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '$', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '$', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '$', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '%', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '%', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '%', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '(', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '(', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '(', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, ')', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, ')', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, ')', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, ':', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, ':', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, ':', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, ';', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, ';', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, ';', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '.', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '.', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '.', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '?', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '?', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '?', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '>', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '>', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '>', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '<', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '<', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '<', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '/', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '/', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '/', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '\\', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '\\', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '\\', '') = ingredients
                   UNION SELECT * from products 
                   join ingred on Replace(product, '|', '') LIKE '% ' || ingredients || '%'
                             OR Replace(product, '|', '') LIKE '%' || ingredients || ' %'
                             OR Replace(product, '|', '') = ingredients
+1

Source: https://habr.com/ru/post/1679998/


All Articles