根据间隔范围内的公共 ID 和日期合并两个数据集

merging two datasets based on common id and date within interval range

我有两个数据集:DF1 - 列出了国家元首 (leader_id) 和国家元首 (country_code) 及其任职时间间隔 (office_interval) 的数据框. DF2 - 数据框,其中每个观察都是一个具有 ID (event_ID) 国家 (country_code) 和发生日期 (event_date)

的事件

数据:

library(lubridate)

#Leader DF
leader_id <- c("Adam","Bob","Charlie","Derek", "Edgar")
country_code <- c(1,1,2,2,3)
office_interval <- c(interval(ymd("1900-01-01"), ymd("1905-01-01")), 
                     interval(ymd("1910-01-01"), ymd("1915-01-01")),
                     interval(ymd("1920-01-01"), ymd("1925-01-01")),
                     interval(ymd("1930-01-01"), ymd("1935-01-01")),
                     interval(ymd("1940-01-01"), ymd("1945-01-01")))
DF1 <- data.frame(leader_id, country_code, office_interval)

#Event DF
event_id <- c(1,1,2,3,3)
country_code <- c(1,2,2,1,3)
event_date <- c(as.Date("1901-01-02"), 
                as.Date("1920-01-02"), 
                as.Date("1921-01-02"),
                as.Date("1911-01-02"),
                as.Date("1941-02-02"))
DF2 <- data.frame(event_id, country_code, event_date)

我想在 DF2 中创建一个新列,它根据 DF2 中出现在同一个国家/地区的领导人 office_interval 中的每一行从 DF1 中获取 leaderid。

DF2 之后应该是这样的:

  event_id country_code event_date leader_id
1        1            1 1901-01-02      Adam
2        1            2 1920-01-02   Charlie
3        2            2 1921-01-02   Charlie
4        3            1 1911-01-02       Bob
5        3            3 1941-02-02     Edgar

我已经尝试了 here 的一些解决方案,但我无法使它们中的任何一个起作用。

这里有一个解决方案也许可以满足您的需求

idx <- sapply(1:nrow(DF2), function(k) which(DF2$event_date[k] %within% DF1$office_interval & DF2$country_code[k]%in% DF1$country_code))
DF2$leader_id <- DF1$leader_id[idx]

这样

> DF2
  event_id country_code event_date leader_id
1        1            1 1901-01-02      Adam
2        1            2 1920-01-02   Charlie
3        2            2 1921-01-02   Charlie
4        3            1 1911-01-02       Bob
5        3            3 1941-02-02     Edgar

这也应该有效:

# add start and end date
DF1$start_date <- substr(DF1$office_interval, 1, 10)
DF1$end_date <- substr(DF1$office_interval, 17, 26)

# merge dataframes
DF2 <- merge(x = DF2, y = DF1, by.x = "country_code", by.y = "country_code")

# filter for correct times
DF2 <- DF2[(DF2$event_date >= DF2$start_date & DF2$event_date <= DF2$end_date),]

# select columns
DF2[1:4]

我们可以通过"country_code"left_joinDF2DF1,保留时间间隔范围内的记录。

library(dplyr)
library(lubridate)

left_join(DF2, DF1, by = "country_code") %>% 
      filter(event_date %within% office_interval)

#  event_id country_code event_date leader_id                office_interval
#1        1            1 1901-01-02      Adam 1900-01-01 UTC--1905-01-01 UTC
#2        1            2 1920-01-02   Charlie 1920-01-01 UTC--1925-01-01 UTC
#3        2            2 1921-01-02   Charlie 1920-01-01 UTC--1925-01-01 UTC
#4        3            1 1911-01-02       Bob 1910-01-01 UTC--1915-01-01 UTC
#5        3            3 1941-02-02     Edgar 1940-01-01 UTC--1945-01-01 UTC