Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

53 lines
1.4KB

  1. process_report_dates <- function(report_list_raw, cover_raw) {
  2. cover_dates <-
  3. cover_raw |>
  4. select(
  5. report_id,
  6. cover_start_date = date_from,
  7. cover_end_date = date_to,
  8. cover_date_filed = date_filed
  9. )
  10. report_list_raw |>
  11. left_join(reporting_schedule(), by = join_by(year, doc_name)) |>
  12. select(
  13. report_id, sboe_id, year, doc_name, amended,
  14. contains("received_"),
  15. matches("(sboe_)?(start|end)_date")
  16. ) |>
  17. left_join(cover_dates, by = "report_id") |>
  18. mutate(across(matches("received|date"), na_if_obviously_wrong_date)) |>
  19. mutate(
  20. # If the received date isn't after at least one of the report or cover date, don't believe it
  21. received_image = received_image |> na_if_not_after_one_of(sboe_start_date, cover_start_date),
  22. received_data = received_data |> na_if_not_after_one_of(sboe_start_date, cover_start_date)
  23. )
  24. }
  25. na_if_obviously_wrong_date <- function(x) {
  26. x[x > today()] <- NA_Date_
  27. x[x < ymd("2016-01-01")] <- NA_Date_
  28. x
  29. }
  30. na_if_not_after_one_of <- function(x, ...) {
  31. others <- list(...)
  32. is_after <- function(x, y) {
  33. ret <- x >= y
  34. ret[is.na(ret)] <- FALSE
  35. ret
  36. }
  37. allow <- purrr::map(others, is_after, x = x) |> purrr::reduce(`|`)
  38. x[!allow] <- NA_Date_
  39. x
  40. }
  41. mean_date_scalar <- function(x, y) {
  42. if (is.na(x) && is.na(y)) {
  43. return(NA_Date_)
  44. }
  45. mean(c(x, y), na.rm = TRUE)
  46. }