【问题标题】:Merging two dataframes by stringmatch with dplyr and stringdist通过 stringmatch 与 dplyr 和 stringdist 合并两个数据帧
【发布时间】:2019-05-02 20:22:33
【问题描述】:

我正在尝试基于非常相似的语言对两个数据帧进行 dplyr 左连接(这不准确)。

DF1:

title | records
Bob's show, part 1 | 42
Time for dinner | 77
Horsecrap | 121

DF2:

showname | counts
Bob's show part 1 | 772
Dinner time | 89
No way Jose | 123

我执行此操作以使用 stringdist 包/库将字符串距离作为向量获取:

titlematch <- amatch(df1$title,df2$showname) 

向量看起来像……嗯,一个整数向量:

titlematch
1
2
NA

通常,如果我有完全匹配,我会这样做:

blended <- left_join(df1, df2, by = c("title" = "showname"))

如何使用向量作为记录选择器进行左连接,以便最终结果为:

title | records | showname | counts
Bob's show, part 1 | 42 | Bob's show part 1 | 772
Time for dinner | 77 | Dinner time | 89

排除第三个不匹配项,因为向量中没有可能的匹配项 (NA)。

【问题讨论】:

  • 您可以使用 tidyr::crossing() 制作笛卡尔积,然后对其进行过滤 - 如果数据集很大,则成本很高
  • 你看过fuzzyjoin吗?

标签: r dplyr stringdist


【解决方案1】:

这是一个镜头,

library(stringdist)
library(tidyverse)

df1 %>%
  as_tibble() %>%
  mutate(temp = amatch(title, df2$showname, maxDist = 10)) %>%
  bind_cols(df2[.$temp, ]) %>%
  select(-temp)

# A tibble: 3 x 4
  title              records showname          counts
  <chr>                <int> <chr>              <int>
1 Bob's show, part 1      42 Bob's show part 1    772
2 Time for dinner         77 Dinner time           89
3 Horsecrap              121 Dinner time           89

我无法重现你的数字匹配向量,amatch(df1$title, df2$showname) 给了我[1] NA NA NA,因为它看起来默认是 0.1,所以我将 maxDist 设置为 10。

最后,您可以随时添加 %&gt;% filter(is.na(showname)) 以删除任何不匹配的行。

数据

df1 <- structure(list(title = c("Bob's show, part 1", "Time for dinner", 
"Horsecrap"), records = c(42L, 77L, 121L)), .Names = c("title", 
"records"), row.names = c(NA, -3L), class = "data.frame")

df2 <- structure(list(showname = c("Bob's show part 1", "Dinner time", 
"No way Jose"), counts = c(772L, 89L, 123L)), .Names = c("showname", 
"counts"), row.names = c(NA, -3L), class = "data.frame")

【讨论】:

    【解决方案2】:

    camille suggested in a comment:

    你看过fuzzyjoin吗?

    我以前从未听说过fuzzyjoin,但我尝试过并喜欢它。 stringdist_left_join 正是我所需要的。

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2021-12-28
      • 2021-09-10
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多