Limit Output of max_overlap Analysis to Instances that are Part of the max_overlap Group(s)

26 Views Asked by At

Several weeks back, participants on the forum helped me to do the following:

library(tidyverse)

book_id <- c("B", "B", "B", "B", "B", "B")
start_date <- ymd(c("2017-11-03", "2017-11-14", "2019-04-01", "2021-05-11",
                    "2022-01-09", "2022-07-11"))
end_date <- ymd(c("2017-12-06", "2017-12-18", "2019-04-12", "2023-01-05", "2022-12-12", 
                  "2023-05-22"))
interval_date <- interval(start_date, end_date)
df_test <- data.frame(book_id, start_date, end_date, interval_date)


df_test_2 <- df_test %>%
  split(df_test$book_id) %>%
  map_dfr(\(x) mutate(x, max_overlap = max(map_int(seq(min(start_date), max(end_date), 1),
                                                   \(d) sum(d %within% x$interval_date)))))

"B" represents any number of copies of a book and periods that they went out on loan from a library consortium. The code looks at the start and end dates of each loan and determines the maximum number of copies that were on loan at the same time.

Beautiful!

What I'm wondering is if there is any way to refine the code so that the only rows output are those together make up the maximum overlap (or overlaps if there are multiple windows when the max_overlap count occurred). In this case, the only rows output would be the final 3.

Thanks in advance!

Joe_T

0

There are 0 best solutions below