gpt4 book ai didi

r - 检查一个数据框中的日期是否在另一个数据框中的日期范围内,并在为真时返回行

转载 作者:行者123 更新时间:2023-12-02 17:01:42 25 4
gpt4 key购买 nike

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 解决方案(首选)或其他方式来加速我的代码。

最佳答案

自 v1.9.8 版(2016 年 11 月 25 日在 CRAN 上)起,data.table 已获得执行非等价连接的能力。

此处,非等值更新联接 用于附加列 RoundStart.DateEnd。从 Sample.DatesRef.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

关于r - 检查一个数据框中的日期是否在另一个数据框中的日期范围内,并在为真时返回行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853400/

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