如何对在另一个数据集的时间间隔内发生的每个 ID 的一个数据集中的总观察值求和
How to sum total observations in one dataset per ID that occur within time interval of another dataset
我有两个数据集:DF1 - 列出了国家元首 (leader_id) 和国家元首 (country_code) 及其任职时间间隔 (office_interval) 的数据框. DF2 - 数据框,其中每个观察结果都是一个事件,其中包含国家 (country_code) 和发生日期 (event_date)。
可重现的数据:
library(lubridate)
#Leader DF
leader_id <- c("Adam","Bob","Charlie")
country_code <- c(1,1,2)
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")))
DF1 <- data.frame(leader_id, country_code, office_interval)
#Event DF
country_code <- c(1,2,2,1)
event_date <- c(as.Date("1901-01-01"),
as.Date("1902-01-01"),
as.Date("1921-01-01"),
as.Date("1901-02-02"))
DF2 <- data.frame(country_code, event_date)
我想创建一个新列 DF1$total_events,用于汇总 DF2 中每个领导者在相同 country_code 和 office_interval 内发生的观察总数在DF1中。它应该是这样的:
leader_id country_code1 office_interval total_events
1 Adam 1 1900-01-01 UTC--1905-01-01 UTC 2
2 Bob 1 1910-01-01 UTC--1915-01-01 UTC 0
3 Charlie 2 1920-01-01 UTC--1925-01-01 UTC 1
我尝试修改 中的一些解决方案,但是我无法对我的数据进行任何处理。
我们可以通过 "country_code"
在 DF1
和 DF2
上做一个 left_join
并计算 office_interval
内 event_date
的数量。
library(dplyr)
library(lubridate)
DF1 %>%
left_join(DF2, by = "country_code") %>%
group_by(leader_id, country_code, office_interval) %>%
summarise(total_events = sum(event_date %within% office_interval))
# leader_id country_code office_interval total_events
# <fct> <dbl> <Interval> <int>
#1 Adam 1 1900-01-01 UTC--1905-01-01 UTC 2
#2 Bob 1 1900-01-01 UTC--1905-01-01 UTC 0
#3 Charlie 2 1910-01-01 UTC--1915-01-02 UTC 1
使用data.table
library(data.table)
library(lubridate)
setDT(DF1)[DF2, on = .(country_code)][, .(total_events =
sum(event_date %within% office_interval)),
.(leader_id, country_code, new = office_interval)]
我有两个数据集:DF1 - 列出了国家元首 (leader_id) 和国家元首 (country_code) 及其任职时间间隔 (office_interval) 的数据框. DF2 - 数据框,其中每个观察结果都是一个事件,其中包含国家 (country_code) 和发生日期 (event_date)。
可重现的数据:
library(lubridate)
#Leader DF
leader_id <- c("Adam","Bob","Charlie")
country_code <- c(1,1,2)
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")))
DF1 <- data.frame(leader_id, country_code, office_interval)
#Event DF
country_code <- c(1,2,2,1)
event_date <- c(as.Date("1901-01-01"),
as.Date("1902-01-01"),
as.Date("1921-01-01"),
as.Date("1901-02-02"))
DF2 <- data.frame(country_code, event_date)
我想创建一个新列 DF1$total_events,用于汇总 DF2 中每个领导者在相同 country_code 和 office_interval 内发生的观察总数在DF1中。它应该是这样的:
leader_id country_code1 office_interval total_events
1 Adam 1 1900-01-01 UTC--1905-01-01 UTC 2
2 Bob 1 1910-01-01 UTC--1915-01-01 UTC 0
3 Charlie 2 1920-01-01 UTC--1925-01-01 UTC 1
我尝试修改
我们可以通过 "country_code"
在 DF1
和 DF2
上做一个 left_join
并计算 office_interval
内 event_date
的数量。
library(dplyr)
library(lubridate)
DF1 %>%
left_join(DF2, by = "country_code") %>%
group_by(leader_id, country_code, office_interval) %>%
summarise(total_events = sum(event_date %within% office_interval))
# leader_id country_code office_interval total_events
# <fct> <dbl> <Interval> <int>
#1 Adam 1 1900-01-01 UTC--1905-01-01 UTC 2
#2 Bob 1 1900-01-01 UTC--1905-01-01 UTC 0
#3 Charlie 2 1910-01-01 UTC--1915-01-02 UTC 1
使用data.table
library(data.table)
library(lubridate)
setDT(DF1)[DF2, on = .(country_code)][, .(total_events =
sum(event_date %within% office_interval)),
.(leader_id, country_code, new = office_interval)]