Skip to content

Commit

Permalink
feat: add vectorized date generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MMenchero committed Oct 9, 2024
1 parent 9c6522c commit 7d5a067
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions R/generate_output_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
#' dates_df <- .generate_output_dates(df_info, freq, h)
#' }
#'
.generate_output_dates <- function(df_info, freq, h){

new_dates <- vector("list", nrow(df_info))
r_freq <- .r_frequency(freq)

for(i in 1:nrow(df_info)){
.generate_output_dates <- function(df_info, freq, h) {
new_dates <- lapply(1:nrow(df_info), function(i) {
start_date <- df_info$dates[i]
r_freq <- .r_frequency(freq)

if(freq %in% c("QE", "Q")){
dt <- seq(from = start_date, by = r_freq, length.out = h+1)
if (freq %in% c("QE", "Q")) {
# End of quarter dates are: "YYYY-03-31", "YYYY-06-30", "YYYY-09-30", "YYYY-12-31".
dt <- seq(from = start_date, by = "quarter", length.out = h + 1)
month <- lubridate::month(start_date)
dt <- seq(from = start_date, by = "quarter", length.out = h+1)

# Calendar adjustments
if (month %in% c(3, 12)) {
Expand All @@ -44,21 +41,19 @@
}
}
}

}else if(freq %in% c("ME", "M")){
start_date <- start_date+lubridate::days(1)
dt <- seq(from = start_date, by = r_freq, length.out = h+1)-lubridate::days(1)
}else{
dt <- seq(df_info$dates[i], by = r_freq, length.out = h+1)
} else if (freq %in% c("ME", "M")) {
dt <- seq(from = start_date + lubridate::days(1), by = r_freq, length.out = h + 1) - lubridate::days(1)
} else {
dt <- seq(from = start_date, by = r_freq, length.out = h + 1)
}

new_dates[[i]] <- dt[2:length(dt)]
}
dt[2:length(dt)]
})

dates_df <- data.frame(lapply(new_dates, as.POSIXct))

ids <- df_info$unique_id
if(inherits(df_info$unique_id, "numeric") | inherits(df_info$unique_id, "integer")){
if (inherits(df_info$unique_id, "numeric") | inherits(df_info$unique_id, "integer")) {
ids <- as.character(df_info$unique_id)
}
names(dates_df) <- ids
Expand Down

0 comments on commit 7d5a067

Please sign in to comment.