我试图基于非常相似的语言对两个数据文件执行dplyr左连接(这并不准确)。
DF1:
title | records
Bob's show, part 1 | 42
Time for dinner | 77
Horsecrap | 121DF2:
showname | counts
Bob's show part 1 | 772
Dinner time | 89
No way Jose | 123我执行此操作是为了使用字符串包/库作为向量获取字符串距离:
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)中没有可能匹配,所以排除了第三次不匹配。
发布于 2019-05-02 23:47:44
camille suggested in a comment
你看过
fuzzyjoin吗?
我以前从未听说过fuzzyjoin,但我试过并且喜欢它。stringdist_left_join正是我所需要的。
发布于 2019-05-02 21:06:58
这是一次机会,
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。
最后,您可以始终添加%>% 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")https://stackoverflow.com/questions/55959725
复制相似问题