这是我的第一篇帖子/问题,所以要善待。 我有这样的数据框:
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
我有一个像这样的查找表:
ingredients
1 wax
2 salt
3 wood
4 my tears
5 unicorn powder
6 gelatin
7 tin
8 hydrangia leaves
9 spearmint
10 bacon
我想在整个字符串上合并它们,所以我明白了:
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
相反,我得到了这个(通知第7行不需要):
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
我非常痛苦地接近,但我与“明胶”错误地匹配了#39;用' tin'。我希望匹配整个单词,而不是单词的一部分。我尝试了很多不同的技术,最接近的就是:
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 || '%'")
真的很感激任何建议。也许需要一种完全不同的方法?我也欢迎有关问题质量的建议,这是我的第一次,所以你最好马上让我直接。
答案 0 :(得分:3)
使用fuzzyjoin包的解决方案,以及来自stringr的str_detect
:
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)
答案 1 :(得分:1)
考虑在关键字之前或之后为一个空格添加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