我正在使用两个不同的数据集,我想根据阈值合并它们。假设两个数据框如下所示:
library(dplyr)
library(fuzzyjoin)
library(lubridate)
df1 = data_frame(Item=1:5,
DateTime=c("2015-01-01 11:12:14", "2015-01-02 09:15:23",
"2015-01-02 15:46:11", "2015-04-19 22:11:33",
"2015-06-10 07:00:00"),
Count=c(1, 6, 11, 15, 9),
Name="Sterling",
Friend=c("Pam", "Cyril", "Cheryl", "Mallory", "Lana"))
df1$DateTime = ymd_hms(df1$DateTime)
df2 = data_frame(Item=21:25,
DateTime=c("2015-01-01 11:12:15", "2015-01-02 19:15:23",
"2015-01-02 15:46:11", "2015-05-19 22:11:33",
"2015-06-10 07:00:02"),
Count=c(3, 7, 11, 15, 8),
Name="Sterling",
Friend=c("Pam", "Kreger", "Woodhouse", "Gillete", "Lana"))
df2$DateTime = ymd_hms(df2$DateTime)
我现在想要的是能够基于模糊匹配并在它们各自值的两秒内离开加入,而除此之外的所有其他值df2
都是相同的。我想我可以通过以下方式到达那里:df1
DateTime
Count
Item
df1 %>%
difference_left_join(df2, by=c("DateTime", "Count"), max_dist=2)
但这给了我以下输出:
# A tibble: 8 × 10
Item.x DateTime.x Count.x Name.x Friend.x Item.y DateTime.y Count.y Name.y Friend.y
<int> <dttm> <dbl> <chr> <chr> <int> <dttm> <dbl> <chr> <chr>
1 1 2015-01-01 11:12:14 1 Sterling Pam 21 2015-01-01 11:12:15 3 Sterling Pam
2 1 2015-01-01 11:12:14 1 Sterling Pam 21 2015-01-01 11:12:15 3 Sterling Pam
3 2 2015-01-02 09:15:23 6 Sterling Cyril NA <NA> NA <NA> <NA>
4 3 2015-01-02 15:46:11 11 Sterling Cheryl 23 2015-01-02 15:46:11 11 Sterling Woodhouse
5 3 2015-01-02 15:46:11 11 Sterling Cheryl 23 2015-01-02 15:46:11 11 Sterling Woodhouse
6 4 2015-04-19 22:11:33 15 Sterling Mallory NA <NA> NA <NA> <NA>
7 5 2015-06-10 07:00:00 9 Sterling Lana 25 2015-06-10 07:00:02 8 Sterling Lana
8 5 2015-06-10 07:00:00 9 Sterling Lana 25 2015-06-10 07:00:02 8 Sterling Lana
这很接近,除了第 3 行不应该合并,因为名称不同(我希望第 2 行在给定阈值的情况下合并,即使我不希望它这样做)。
我如何最终得到以下数据框?请注意,尽管满足阈值限制,df2
但未合并第二行和第三行。这是因为其他列(除了)不相同。DateTime
Count
Item
desired_output
# Item DateTime Count Name Friend
# 1 3 2015-01-02 15:46:11 11 Sterling Cheryl
# 2 2 2015-01-02 09:15:23 6 Sterling Cyril
# 3 5 2015-06-10 07:00:00 9 Sterling Lana
# 4 25 2015-06-10 07:00:02 8 Sterling Lana
# 5 4 2015-04-19 22:11:33 15 Sterling Mallory
# 6 1 2015-01-01 11:12:14 1 Sterling Pam
# 7 21 2015-01-01 11:12:15 3 Sterling Pam