Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A large speed up to extract_fin_year #96

Merged
merged 5 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# phsmethods (development version)

- `extract_fin_year()` is now much faster and will use less memory, especially for smaller vectors (1 to 1,000).

# phsmethods 0.2.2

- Improve `chi_check()` function to make it more efficient and run faster.
Expand Down
46 changes: 18 additions & 28 deletions R/extract_fin_year.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title Assign a date to a financial year
#' @title Extract the formatted financial year from a date
#'
#' @description \code{extract_fin_year} takes a date and assigns it to the correct
#' financial year in the PHS specified format.
#' @description \code{extract_fin_year} takes a date and extracts the
#' correct financial year in the PHS specified format from it.
#'
#' @details The PHS accepted format for financial year is YYYY/YY e.g. 2017/18.
#'
Expand All @@ -17,7 +17,8 @@
#' @export
extract_fin_year <- function(date) {
if (!inherits(date, c("Date", "POSIXct"))) {
cli::cli_abort("{.arg date} must be a {.cls Date} or {.cls POSIXct} vector, not a {.cls {class(date)}} vector.")
cli::cli_abort("{.arg date} must be a {.cls Date} or {.cls POSIXct} vector,
not a {.cls {class(date)}} vector.")
}

# Simply converting all elements of the input vector resulted in poor
Expand All @@ -26,29 +27,18 @@ extract_fin_year <- function(date) {
# and then match them back on to the original input. This vastly improves
# performance for large inputs.

x <- tibble::tibble(dates = unique(date)) %>%
dplyr::mutate(
fyear = paste0(
ifelse(lubridate::month(.data$dates) >= 4,
lubridate::year(.data$dates),
lubridate::year(.data$dates) - 1
),
"/",
substr(
ifelse(lubridate::month(.data$dates) >= 4,
lubridate::year(.data$dates) + 1,
lubridate::year(.data$dates)
),
3, 4
)
),
fyear = ifelse(is.na(.data$dates),
NA_character_,
.data$fyear
)
)
unique_date <- unique(date)

tibble::tibble(dates = date) %>%
dplyr::left_join(x, by = "dates") %>%
dplyr::pull(.data$fyear)
unique_fy_q <-
lubridate::year(unique_date) - (lubridate::month(unique_date) %in% 1:3)

unique_fy <- ifelse(
is.na(unique_date),
NA_character_,
paste0(unique_fy_q, "/", (unique_fy_q %% 100L) + 1L)
)

fin_years <- unique_fy[match(date, unique_date)]

return(fin_years)
}
6 changes: 3 additions & 3 deletions man/extract_fin_year.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.