[英]Merge two data frames by nearest date and ID
我有兩個數據框
A:
customer application_date
1 2010-05-08
2 2012-08-08
3 2013-06-23
和乙:
customer date balance
1 2009-01-02 2500
1 2009-12-24 3000
2 2011-11-11 1200
2 2012-05-20 1900
3 2013-03-21 5500
3 2013-06-05 4500
我需要將它們合並在一起,以便表 A 中的日期最接近表 B 中的日期(按客戶)
結果:
customer application_date date balance
1 2010-05-08 2009-12-24 3000
2 2012-08-08 2012-05-20 1900
3 2013-06-23 2013-06-05 4500
我怎樣才能正確合並這兩個表?
示例表的代碼:
A <- data.frame(customer = c(1,2,3),
application_date = c("2010-05-08", "2012-08-08", "2013-06-23"))
B <- data.frame(customer = c(1,1,2,2,3,3),
date = c("2009-01-02", "2009-12-24", "2011-11-11", "2012-05-20", "2013-03-21", "2013-06-05"),
balance = c(2500, 3000, 1200, 1900, 5500, 4500))
這是滾動連接的一種選擇
library(data.table)
setDT(B)[, application_date := date]
B[A, on = .(customer, date = application_date), roll = 'nearest']
# customer date balance application_date
#1: 1 2010-05-08 3000 2009-12-24
#2: 2 2012-08-08 1900 2012-05-20
#3: 3 2013-06-23 4500 2013-06-05
A$application_date <- as.Date(A$application_date)
B$date <- as.Date(B$date)
這是在 R 中使用 SQL 的解決方案。 如果原始數據很大並且來自數據庫,SQL/HiveQL 可能會很有用。
require(sqldf)
# convert date
A$application_date = as.Date(A$application_date)
B$date = as.Date(B$date)
# find all possible intervals for each customer
AB=sqldf("select A.customer, A.application_date, B.date, B.balance, abs(A.application_date-B.date) as diff_day
from A,B
where A.customer = B.customer")
# extract the min interval for each customer
AB_min=sqldf("select customer, min(diff_day) as min_diff_day
from AB
group by 1")
# link the min one to the customer and remove irrelevant rows
AB_match=sqldf("select AB.customer, AB.application_date, AB.date, AB.balance
from AB_min
left join AB
on AB_min.customer = AB.customer
and AB_min.min_diff_day = AB.diff_day
group by 1")
AB_match 是最終的 output。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.