微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用R在2列之间匹配字符串/文本值

希望阅读此消息的每个人都做得很好:)

我正在尝试在3M记录的数据集中识别将自己报告为买方的卖方。因此,我试图匹配两个不同列中的名称,即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"))

我通过使用str_detect()进行了尝试,但是它只是返回了A列中完全包含在B列中的值:

sales_table_Mtchd <- sales_table %>%
mutate(Match = str_detect(sales_table$`BuyerName`,fixed(sales_table$`SellerName`,ignore_case = TRUE)))

接下来,我将举例说明我要获得的东西:

expected outcome using R (full format)

*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

非常感谢您提前提供帮助和提示

解决方法

使用 const sortResponseData = arr1.sort((a: any,b: any) => { return a.name > b.name ? 1 : b.name > a.name ? -1 : 0; }); const sortEntityMetaData = arr2.sort((a: any,b: any) => { return a.name > b.name ? 1 : b.name > a.name ? -1 : 0; }); expect(sortResponseData).toEqual(sortEntityMetaData);

intersect

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。