gpt4 book ai didi

r - 如何在 R 中使用 Fuzzyjoin::difference_* 执行模糊连接

转载 作者:行者123 更新时间:2023-12-04 15:08:33 25 4
gpt4 key购买 nike

我正在处理两个不同的数据集,我想根据阈值合并这些数据集。假设两个数据框如下所示:

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)

我现在想要的,是可以离开加入 df2df1基于 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

最佳答案

好的,所以,您收到的消息是因为无法在非数字列上计算模糊匹配。

要做的是将其转换为数字。由于您的卡尺以秒为单位,因此我将其转换为秒,然后将它们设为数字:

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$DateTime1 = as.numeric(seconds(ymd_hms(df1$DateTime)))

df2 = data_frame(Item=21:25,
DateTime=c("2015-01-01 11:12:15", "2015-01-02 19:25:56",
"2015-01-02 15:46:11", "2015-05-19 22:11:33",
"2015-06-10 07:00:02"),
Count=c(3, 6, 11, 15, 8),
Name="Sterling",
Friend=c("Pam", "Kreger", "Woodhouse", "Gillete", "Lana"))
df2$DateTime1 = as.numeric(seconds(ymd_hms(df2$DateTime)))

df1 %>%
difference_left_join(y=df2, by=c("DateTime1", "Count"), max_dist=2)

根据我们在评论中的讨论,将其子集化为其他字符列匹配的情况的简单调整是:
df1[df2$Friend == df1$Friend,] %>%
difference_left_join(y=df2[df2$Friend == df1$Friend,], by=c("DateTime1", "Count"), max_dist=2)

该示例仅适用于 Friend但当然你可以使用 &用多列来做。

关于r - 如何在 R 中使用 Fuzzyjoin::difference_* 执行模糊连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39644784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com