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分かかります。整頓された解決策(推奨)または他の手段を使用して私のコードを高速化する方法を誰かが見ることができるかどうかを検索します。
バージョンv1.9.8(2016年11月25日CRAN)の時点で、非等価結合data.table
を実行する機能が追加されました。
ここでは、非エクイ更新ジョイン列を追加するために使用されるRound
、Start.Date
とEnd.Date
からSample.Dates
へ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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加