@@ -2252,7 +2252,7 @@ friedman_test <- function(series, frequency = 12) {
22522252# ' - Positive integer or vector (e.g., 0 for first release, 1 for second, etc.)
22532253# ' - `"first"` to extract the first release.
22542254# ' - `"latest"` to extract the most recent release.
2255- # ' Default is 1 (the first release).
2255+ # ' Default is 0 (the first release).
22562256# ' @param diagonal Logical. If `TRUE`, the function only returns real
22572257# ' first releases.
22582258# '
@@ -2338,22 +2338,21 @@ get_nth_release <- function(df, n = 0, diagonal = FALSE) {
23382338 nth_release <- get_first_release(df )
23392339 }
23402340 if (diagonal ) {
2341- min_pub_date <- df %> %
2342- dplyr :: group_by(" id" ) %> %
2343- dplyr :: summarize(min_pub_date = min(.data $ pub_date )) %> %
2344- dplyr :: pull(" min_pub_date" , " id" ) %> %
2345- as.Date()
2346-
2347- max_time <- df %> %
2348- dplyr :: filter(.data $ pub_date == min_pub_date ) %> %
2341+ diagonal_thresholds <- df %> %
23492342 dplyr :: group_by(.data $ id ) %> %
2350- dplyr :: summarize(max_time = max(.data $ time )) %> %
2351- dplyr :: pull(" max_time" , " id" ) %> %
2352- as.Date()
2343+ dplyr :: summarise(min_pub_date = min(.data $ pub_date ), .groups = " drop" ) %> %
2344+ dplyr :: left_join(
2345+ df %> %
2346+ dplyr :: group_by(.data $ id , .data $ pub_date ) %> %
2347+ dplyr :: summarise(max_time = max(.data $ time ), .groups = " drop" ),
2348+ by = c(" id" , " min_pub_date" = " pub_date" )
2349+ ) %> %
2350+ dplyr :: select(.data $ id , .data $ max_time )
23532351
2354- # Filter using direct vectorized lookup
2355- df <- df %> %
2356- dplyr :: filter(.data $ time > = max_time [.data $ id ])
2352+ nth_release <- nth_release %> %
2353+ dplyr :: left_join(diagonal_thresholds , by = " id" ) %> %
2354+ dplyr :: filter(.data $ time > = .data $ max_time ) %> %
2355+ dplyr :: select(- " max_time" )
23572356 }
23582357 } else {
23592358 # Ensure data is sorted by pub_date and time
@@ -2380,7 +2379,7 @@ get_nth_release <- function(df, n = 0, diagonal = FALSE) {
23802379 if (diagonal ) {
23812380 min_pub_date <- min(df $ pub_date )
23822381 max_time <- max(df $ time [df $ pub_date == min_pub_date ])
2383- df <- df %> %
2382+ nth_release <- nth_release %> %
23842383 dplyr :: filter(
23852384 .data $ time > = max_time
23862385 )
@@ -2508,7 +2507,7 @@ get_latest_release <- function(df) {
25082507 dplyr :: arrange(.data $ id , .data $ pub_date , .data $ time )
25092508 df <- df %> %
25102509 dplyr :: group_by(.data $ id , .data $ time ) %> %
2511- dplyr :: mutate(" release" = paste0(" release_" , dplyr :: n())) %> %
2510+ dplyr :: mutate(" release" = paste0(" release_" , dplyr :: n() - 1 )) %> %
25122511 dplyr :: filter(.data $ pub_date == max(.data $ pub_date )) %> %
25132512 dplyr :: ungroup()
25142513 } else {
@@ -2517,7 +2516,7 @@ get_latest_release <- function(df) {
25172516 dplyr :: arrange(.data $ pub_date , .data $ time )
25182517 df <- df %> %
25192518 dplyr :: group_by(.data $ time ) %> %
2520- dplyr :: mutate(" release" = paste0(" release_" , dplyr :: n())) %> %
2519+ dplyr :: mutate(" release" = paste0(" release_" , dplyr :: n() - 1 )) %> %
25212520 dplyr :: filter(.data $ pub_date == max(.data $ pub_date )) %> %
25222521 dplyr :: ungroup()
25232522 }
@@ -2538,10 +2537,12 @@ get_latest_release <- function(df) {
25382537# ' time` (observation date).
25392538# ' @param month An optional parameter specifying the target month as a name
25402539# ' ("July") or an integer (7). Cannot be used with `quarter`.
2540+ # ' At least one of `month` or `quarter` must be supplied.
25412541# ' @param quarter An optional parameter specifying the target quarter (1-4).
2542- # ' Cannot be used with `month`.
2543- # ' @param years The integer number of unrestricted years after `pub_date` for
2544- # ' which the values should be extracted.
2542+ # ' Cannot be used with `month`. At least one of `month` or `quarter` must be
2543+ # ' supplied.
2544+ # ' @param years A single whole number of years after `pub_date` for which the
2545+ # ' values should be extracted.
25452546# '
25462547# ' @return A filtered data frame containing values matching the
25472548# ' specified criteria.
@@ -2559,26 +2560,45 @@ get_fixed_release <- function(df, years, month = NULL, quarter = NULL) {
25592560 rlang :: abort(" Specify either a month or a quarter, not both." )
25602561 }
25612562
2562- # Ensure years is numeric and integer or can be converted to integer
2563- if (is.numeric(years )) {
2564- if (years %% 1 != 0 ) {
2565- rlang :: abort(" years' must be a whole number." )
2566- }
2567- years <- as.integer(years )
2563+ # Ensure one target period is specified
2564+ if (is.null(month ) && is.null(quarter )) {
2565+ rlang :: abort(" Specify one of 'month' or 'quarter'." )
25682566 }
25692567
2568+ # Ensure years is a single whole number
2569+ if (! is.numeric(years ) || length(years ) != 1 || is.na(years ) || years %% 1 != 0 ) {
2570+ rlang :: abort(" 'years' must be a single whole number." )
2571+ }
2572+ years <- as.integer(years )
2573+
25702574 # Ensure the month is in numeric format if provided
25712575 if (! is.null(month )) {
25722576 if (is.character(month )) {
2577+ if (length(month ) != 1 ) {
2578+ rlang :: abort(" Invalid 'month'. Must be a single month name or integer." )
2579+ }
25732580 month <- match(tolower(month ), tolower(month.name ))
25742581 if (is.na(month )) rlang :: abort(" Invalid 'month' name" )
2582+ } else if (
2583+ ! is.numeric(month ) || length(month ) != 1 || is.na(month ) ||
2584+ month %% 1 != 0 || month < 1 || month > 12
2585+ ) {
2586+ rlang :: abort(
2587+ " Invalid 'month'. Must be an integer between 1 and 12 or a month name."
2588+ )
25752589 }
2590+ month <- as.integer(month )
25762591 }
2592+
25772593 # Ensure quarter is in numeric format if provided
25782594 if (! is.null(quarter )) {
2579- if (! quarter %in% 1 : 4 ) {
2595+ if (
2596+ ! is.numeric(quarter ) || length(quarter ) != 1 || is.na(quarter ) ||
2597+ quarter %% 1 != 0 || ! quarter %in% 1 : 4
2598+ ) {
25802599 rlang :: abort(" Invalid quarter number. Must be between 1 and 4." )
25812600 }
2601+ quarter <- as.integer(quarter )
25822602 }
25832603
25842604 check <- vintages_check(df )
0 commit comments