Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

54 lines
1.4KB

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