使用R在两列之间匹配字符串/文本值

使用R在两列之间匹配字符串/文本值,r,matching,R,Matching,希望每个读到这条消息的人都做得很好:) 我试图在一个包含300万条记录的数据集中识别那些自称为买家的卖家。因此,我试图匹配两个不同列中的名称,列A=SellerName和列B=BuyerName。列A名称被清理并正确派生,但列B包含卖家报告的原始数据,因此我的目标是使用列A作为参考值,并将它们与列B匹配,在新列中获得布尔值(TRUE或FALSE)作为结果,并接受部分匹配 数据集: library(data.table) sales_table <- data.table(Selle

希望每个读到这条消息的人都做得很好:)

我试图在一个包含300万条记录的数据集中识别那些自称为买家的卖家。因此,我试图匹配两个不同列中的名称,列A=SellerName和列B=BuyerName。列A名称被清理并正确派生,但列B包含卖家报告的原始数据,因此我的目标是使用列A作为参考值,并将它们与列B匹配,在新列中获得布尔值(TRUE或FALSE)作为结果,并接受部分匹配

数据集:

library(data.table)
    sales_table <- data.table(SellerName = c("Partner ABC INC", "Partner CPE Corp", "Partner TYS Ltd", "Partner MDW INC"), BuyerName = c("Subsidiary abc, Boston", "John Smith, CPA", "Tech tYs, East Coast", "partner mdw Inc"))
接下来,我将举一个我想要得到的例子:


我非常感谢您的帮助和提示

使用
intersect

require(tidyverse)
df = tibble(
    SellerName = c('Partner ABC INC', 'Partner CPE Corp', 'Partner TYS Ltd', 'Partner MDW INC'),
    BuyerName = c('Subsidiary abc, Boston', 'John Smith, CPA', 'Tech tYs, East Coast', 'partner mdw inc')
)

df_match = df %>% mutate(SellerName_ls = SellerName %>% tolower() %>% str_replace_all(',', '') %>% str_split(pattern = ' '),
              BuyerName_ls = BuyerName %>% tolower() %>% str_replace_all(',', '') %>% str_split(pattern = ' '))

df_match$match_string = map2(df_match$SellerName_ls, df_match$BuyerName_ls, intersect) %>% map_chr(str_c, collapse = ' ')

df_match = df_match %>% mutate(matched = ifelse(str_length(match_string) > 0, T, F))

请使用
dput
或我们可以复制和使用的东西添加数据。读一下,完成!对不起,我已经习惯了论坛规则:)嘿,伙计,非常感谢你的快速回复!只是一个简单的问题,你在哪里定义了“匹配字符串”?没关系,这是我的打字错误。非常感谢你!!
*SellerName*        *BuyerName*              *Match*
Partner ABC INC     Subsidiary abc, Boston   TRUE
Partner CPE Corp    John Smith, CPA          FALSE
Partner TYS Ltd     Tech tYs, East Coast     TRUE
Partner MDW INC     partner mdw Inc          TRUE
require(tidyverse)
df = tibble(
    SellerName = c('Partner ABC INC', 'Partner CPE Corp', 'Partner TYS Ltd', 'Partner MDW INC'),
    BuyerName = c('Subsidiary abc, Boston', 'John Smith, CPA', 'Tech tYs, East Coast', 'partner mdw inc')
)

df_match = df %>% mutate(SellerName_ls = SellerName %>% tolower() %>% str_replace_all(',', '') %>% str_split(pattern = ' '),
              BuyerName_ls = BuyerName %>% tolower() %>% str_replace_all(',', '') %>% str_split(pattern = ' '))

df_match$match_string = map2(df_match$SellerName_ls, df_match$BuyerName_ls, intersect) %>% map_chr(str_c, collapse = ' ')

df_match = df_match %>% mutate(matched = ifelse(str_length(match_string) > 0, T, F))