检查一个数据框中的日期是否在另一个数据框中的日期范围内,如果为真,则检查 return 行
Check date in one dataframe is in date range in another dataframe and return rows when true
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, lubridate)
# Example of sample dates - these are to be used to cross check if date exists within the range
Sample.Dates = tibble(
ID = "ID",
Round = 1:3,
Start.Date = dmy(c("03/12/2018","10/12/2018","17/12/2018")),
End.Date = dmy(c("09/12/2018","16/12/2018","23/12/2018")))
# Reference dates for a particular player - "John". Need to cross check the date against Sample.Dates and attach round, start and end date columns
Ref.Dates = tibble(
ID= "ID",
Date = seq.Date(ymd("2018-12-05"), ymd("2018-12-31") , by = "day"),
Player = "John",
Rows = row_number(Date))
# Function for checking if date exists within range and then returns the round, start and end date values
Dates.Check.YN.Func = function(x){
Date = x %>% pull(Date)
Cross.Check = Sample.Dates %>% rowwise()%>%
dplyr::mutate(Match = ifelse(between(Date, Start.Date, End.Date),1,0))%>%
filter(Match == 1)%>%
ungroup()%>%
select(-Match)
left_join(x, Cross.Check, by = "ID")
}
# Applying function to each row/date using nest()
Data.Nest = Ref.Dates %>%
nest(-Rows)%>%
mutate(out = map(data,Dates.Check.YN.Func)) %>%
unnest(out) %>%
select(-data)
现在这段代码可以正常工作了。然而,这只是一个虚拟数据集,实际上我想交叉检查超过 100,000 个日期。使用我的真实数据集执行此操作需要大约 30 分钟。搜索看看是否有人可以使用 tidyverse 解决方案(首选)或其他方式来加速我的代码。
您可以使用专为此类分析设计的 data.table::foverlaps
。
library(data.table)
library(dtplyr) # allows you to use dplyr with data.table backend
# make Ref.Dates into a data.table
setDT(Ref.Dates)
Ref.Dates[,Date_copy := copy(Date)]
# or dplyr syntax if you prefer
# Ref.Dates = Ref.Dates %>%
# mutate(Date_copy = copy(Date))
# you must make Sample.Dates into a data.table and index by the join keys
setDT(Sample.Dates)
setkey(Sample.Dates, ID, Start.Date, End.Date)
# fast overlaps
Data.Nest = foverlaps(Ref.Dates, Sample.Dates,
by.x = c("ID", "Date", "Date_copy"),
by.y = c("ID", "Start.Date", "End.Date"))
# remove the Date_copy column
Data.Nest[,Date_copy := NULL]
从版本 v1.9.8(2016 年 11 月 25 日在 CRAN 上)开始,data.table
获得了执行 非等值连接的能力。
此处,非等值更新联接用于从[=追加列Round
、Start.Date
和End.Date
16=] 到 Ref.Dates
。 Ref.Dates
通过引用更新,即不复制整个数据对象。
library(data.table)
# coerce to data.table class
setDT(Ref.Dates)[
# perform update join
setDT(Sample.Dates), on = .(ID, Date >= Start.Date, Date <= End.Date),
`:=`(Round = Round, Start.Date = Start.Date, End.Date = End.Date)]
Ref.Dates
ID Date Player Rows Round Start.Date End.Date
1: ID 2018-12-05 John 1 1 2018-12-03 2018-12-09
2: ID 2018-12-06 John 2 1 2018-12-03 2018-12-09
3: ID 2018-12-07 John 3 1 2018-12-03 2018-12-09
4: ID 2018-12-08 John 4 1 2018-12-03 2018-12-09
5: ID 2018-12-09 John 5 1 2018-12-03 2018-12-09
6: ID 2018-12-10 John 6 2 2018-12-10 2018-12-16
7: ID 2018-12-11 John 7 2 2018-12-10 2018-12-16
8: ID 2018-12-12 John 8 2 2018-12-10 2018-12-16
9: ID 2018-12-13 John 9 2 2018-12-10 2018-12-16
10: ID 2018-12-14 John 10 2 2018-12-10 2018-12-16
11: ID 2018-12-15 John 11 2 2018-12-10 2018-12-16
12: ID 2018-12-16 John 12 2 2018-12-10 2018-12-16
13: ID 2018-12-17 John 13 3 2018-12-17 2018-12-23
14: ID 2018-12-18 John 14 3 2018-12-17 2018-12-23
15: ID 2018-12-19 John 15 3 2018-12-17 2018-12-23
16: ID 2018-12-20 John 16 3 2018-12-17 2018-12-23
17: ID 2018-12-21 John 17 3 2018-12-17 2018-12-23
18: ID 2018-12-22 John 18 3 2018-12-17 2018-12-23
19: ID 2018-12-23 John 19 3 2018-12-17 2018-12-23
20: ID 2018-12-24 John 20 NA <NA> <NA>
21: ID 2018-12-25 John 21 NA <NA> <NA>
22: ID 2018-12-26 John 22 NA <NA> <NA>
23: ID 2018-12-27 John 23 NA <NA> <NA>
24: ID 2018-12-28 John 24 NA <NA> <NA>
25: ID 2018-12-29 John 25 NA <NA> <NA>
26: ID 2018-12-30 John 26 NA <NA> <NA>
27: ID 2018-12-31 John 27 NA <NA> <NA>
ID Date Player Rows Round Start.Date End.Date
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, lubridate)
# Example of sample dates - these are to be used to cross check if date exists within the range
Sample.Dates = tibble(
ID = "ID",
Round = 1:3,
Start.Date = dmy(c("03/12/2018","10/12/2018","17/12/2018")),
End.Date = dmy(c("09/12/2018","16/12/2018","23/12/2018")))
# Reference dates for a particular player - "John". Need to cross check the date against Sample.Dates and attach round, start and end date columns
Ref.Dates = tibble(
ID= "ID",
Date = seq.Date(ymd("2018-12-05"), ymd("2018-12-31") , by = "day"),
Player = "John",
Rows = row_number(Date))
# Function for checking if date exists within range and then returns the round, start and end date values
Dates.Check.YN.Func = function(x){
Date = x %>% pull(Date)
Cross.Check = Sample.Dates %>% rowwise()%>%
dplyr::mutate(Match = ifelse(between(Date, Start.Date, End.Date),1,0))%>%
filter(Match == 1)%>%
ungroup()%>%
select(-Match)
left_join(x, Cross.Check, by = "ID")
}
# Applying function to each row/date using nest()
Data.Nest = Ref.Dates %>%
nest(-Rows)%>%
mutate(out = map(data,Dates.Check.YN.Func)) %>%
unnest(out) %>%
select(-data)
现在这段代码可以正常工作了。然而,这只是一个虚拟数据集,实际上我想交叉检查超过 100,000 个日期。使用我的真实数据集执行此操作需要大约 30 分钟。搜索看看是否有人可以使用 tidyverse 解决方案(首选)或其他方式来加速我的代码。
您可以使用专为此类分析设计的 data.table::foverlaps
。
library(data.table)
library(dtplyr) # allows you to use dplyr with data.table backend
# make Ref.Dates into a data.table
setDT(Ref.Dates)
Ref.Dates[,Date_copy := copy(Date)]
# or dplyr syntax if you prefer
# Ref.Dates = Ref.Dates %>%
# mutate(Date_copy = copy(Date))
# you must make Sample.Dates into a data.table and index by the join keys
setDT(Sample.Dates)
setkey(Sample.Dates, ID, Start.Date, End.Date)
# fast overlaps
Data.Nest = foverlaps(Ref.Dates, Sample.Dates,
by.x = c("ID", "Date", "Date_copy"),
by.y = c("ID", "Start.Date", "End.Date"))
# remove the Date_copy column
Data.Nest[,Date_copy := NULL]
从版本 v1.9.8(2016 年 11 月 25 日在 CRAN 上)开始,data.table
获得了执行 非等值连接的能力。
此处,非等值更新联接用于从[=追加列Round
、Start.Date
和End.Date
16=] 到 Ref.Dates
。 Ref.Dates
通过引用更新,即不复制整个数据对象。
library(data.table)
# coerce to data.table class
setDT(Ref.Dates)[
# perform update join
setDT(Sample.Dates), on = .(ID, Date >= Start.Date, Date <= End.Date),
`:=`(Round = Round, Start.Date = Start.Date, End.Date = End.Date)]
Ref.Dates
ID Date Player Rows Round Start.Date End.Date 1: ID 2018-12-05 John 1 1 2018-12-03 2018-12-09 2: ID 2018-12-06 John 2 1 2018-12-03 2018-12-09 3: ID 2018-12-07 John 3 1 2018-12-03 2018-12-09 4: ID 2018-12-08 John 4 1 2018-12-03 2018-12-09 5: ID 2018-12-09 John 5 1 2018-12-03 2018-12-09 6: ID 2018-12-10 John 6 2 2018-12-10 2018-12-16 7: ID 2018-12-11 John 7 2 2018-12-10 2018-12-16 8: ID 2018-12-12 John 8 2 2018-12-10 2018-12-16 9: ID 2018-12-13 John 9 2 2018-12-10 2018-12-16 10: ID 2018-12-14 John 10 2 2018-12-10 2018-12-16 11: ID 2018-12-15 John 11 2 2018-12-10 2018-12-16 12: ID 2018-12-16 John 12 2 2018-12-10 2018-12-16 13: ID 2018-12-17 John 13 3 2018-12-17 2018-12-23 14: ID 2018-12-18 John 14 3 2018-12-17 2018-12-23 15: ID 2018-12-19 John 15 3 2018-12-17 2018-12-23 16: ID 2018-12-20 John 16 3 2018-12-17 2018-12-23 17: ID 2018-12-21 John 17 3 2018-12-17 2018-12-23 18: ID 2018-12-22 John 18 3 2018-12-17 2018-12-23 19: ID 2018-12-23 John 19 3 2018-12-17 2018-12-23 20: ID 2018-12-24 John 20 NA <NA> <NA> 21: ID 2018-12-25 John 21 NA <NA> <NA> 22: ID 2018-12-26 John 22 NA <NA> <NA> 23: ID 2018-12-27 John 23 NA <NA> <NA> 24: ID 2018-12-28 John 24 NA <NA> <NA> 25: ID 2018-12-29 John 25 NA <NA> <NA> 26: ID 2018-12-30 John 26 NA <NA> <NA> 27: ID 2018-12-31 John 27 NA <NA> <NA> ID Date Player Rows Round Start.Date End.Date