Skip to content

Commit 0eb86a4

Browse files
committed
working 554 calendar
1 parent 8393bdd commit 0eb86a4

File tree

4 files changed

+186
-79
lines changed

4 files changed

+186
-79
lines changed

554.R

Lines changed: 27 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,90 +16,47 @@ min_year <- year(x@datum@min_date)
1616

1717
start_year <- closest_sunday_feb1(min_year)
1818

19-
2019
new_cal <- fpaR:::seq_date_sql(start_date = start_year,end_date=x@datum@max_date,time_unit = "day",con =con ) |>
2120
augment_calendar(.date = date)
2221

22+
# this should generate a type of calendar
23+
# need to validate year logic -- I am getting 53 weeks all the time
2324

2425

25-
## next step add in new year indicator
26-
new_cal |>
27-
dplyr::mutate(
28-
new_week_indicator=if_else(day_of_week==1,1,0)
29-
,cumulative_week_count=cumsum(new_week_indicator)
30-
,quarter_554=case_when(
31-
cumulative_week_count<=14~1
32-
,cumulative_week_count<=28~2
33-
,cumulative_week_count<=42~3
34-
,.default=4
35-
)
36-
,month_554=case_when(
37-
cumulative_week_count<=5~1
38-
,cumulative_week_count<=10~2
39-
,cumulative_week_count<=14~3
40-
,.default=0
41-
)
42-
,.before=1
43-
)
44-
45-
46-
47-
48-
# Example usage:
49-
map(2021:2024,\(x) closest_sunday_feb1(x))
50-
51-
52-
generate_5_5_4_daily <- function(start_date, num_years = 1) {
53-
start_date <- as.Date(start_date)
5426

55-
# 5-5-4 pattern: weeks per month
56-
weeks_per_quarter <- c(5, 5, 4)
57-
weeks_per_year <- rep(weeks_per_quarter, 4) # 12 months
27+
pattern <- "544"
5828

59-
# Total weeks to generate
60-
total_weeks <- length(weeks_per_year) * num_years
6129

62-
# Generate week numbers
63-
week_number <- 1:total_weeks
30+
days_in_week=7
31+
weeks_in_quarter=13
32+
quarters_in_year=4
6433

65-
# Generate fiscal years
66-
fiscal_year <- rep(1:num_years, each = length(weeks_per_year))
67-
68-
# Generate month assignment
69-
month_pattern <- 1:12
70-
month <- rep(month_pattern, times = weeks_per_year) # expand by weeks per month
71-
month <- rep(month, times = num_years) # expand for multiple years
72-
month <- month[1:total_weeks] # truncate if needed
73-
74-
# Generate start dates for each week
75-
week_starts <- start_date + 7 * (week_number - 1)
76-
77-
# Generate daily dates
78-
daily_dates <- unlist(lapply(week_starts, function(d) d + 0:6))
79-
80-
# Repeat week, month, fiscal year info for each day
81-
week_number_daily <- rep(week_number, each = 7)
82-
month_daily <- rep(month, each = 7)
83-
fiscal_year_daily <- rep(fiscal_year, each = 7)
84-
85-
# Return daily calendar
86-
data.frame(
87-
date = daily_dates,
88-
fiscal_year = fiscal_year_daily,
89-
month = month_daily,
90-
week_number = week_number_daily
34+
# out <-
35+
new_cal |>
36+
dbplyr::window_order(date) |>
37+
select(date,day_of_week) |>
38+
dplyr::mutate(
39+
year_index=dplyr::if_else(dplyr::row_number()%%(days_in_week*weeks_in_quarter*quarters_in_year)==1,1,0)
40+
,year_ns=cumsum(year_index)
41+
) |>
42+
dplyr::mutate(
43+
week_index=dplyr::if_else(dplyr::row_number()%%7==1,1,0)
44+
,week_ns=cumsum(week_index)
45+
,.by=year_ns
9146
) |>
92-
mutate(
93-
date=as.Date(date)
47+
create_ns_month(pattern=pattern) |>
48+
dplyr::mutate(
49+
quarter_ns=dplyr::case_when(
50+
month_ns<=3~1
51+
,month_ns<=6~2
52+
,month_ns<=9~3
53+
,.default=4
9454
)
95-
}
55+
) |> collect() |> arrange(date)
9656

97-
# Example usage: daily calendar starting Feb 2, 2020, for 1 year
98-
generate_5_5_4_daily("2020-02-02", num_years = 1)
9957

10058

101-
## next step add in new year indicator
59+
out |> collect() |> arrange(date)
10260

10361

10462

105-
## add in period indicator for that

R/utils-misc.R

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ print_actions_steps <- function(x){
219219
#' date-based attributes.
220220
#' @keywords internal
221221

222-
augment_calendar_tbl <- function(.data,.date){
222+
augment_standard_calendar_tbl <- function(.data,.date){
223223

224224
lubridate::days
225225
# create attibutes
@@ -311,7 +311,7 @@ lubridate::days
311311
#' @return A dbi containing the original data along with all generated
312312
#' date-based attributes.
313313
#' @keywords internal
314-
augment_calendar_dbi <- function(.data,.date){
314+
augment_standard_calendar_dbi <- function(.data,.date){
315315

316316

317317
date_vec <- rlang::as_label(.date)
@@ -357,11 +357,8 @@ augment_calendar_dbi <- function(.data,.date){
357357

358358

359359

360-
361-
362-
363360
#' @title Add Comprehensive Date-Based Attributes to a DBI lazy frame or tibble object
364-
#' @name augment_calendar
361+
#' @name augment_standard_calendar
365362
#' @description
366363
#' This function takes a data frame and a date column and generates a wide set of
367364
#' derived date attributes. These include start/end dates for year, quarter,
@@ -407,7 +404,7 @@ augment_calendar_dbi <- function(.data,.date){
407404
#' @return A dbi or tibble containing the original data along with all generated
408405
#' date-based attributes.
409406
#'
410-
augment_calendar <- function(.data,.date){
407+
augment_standard_calendar <- function(.data,.date){
411408

412409
data_class <- class(.data)
413410

@@ -420,7 +417,7 @@ augment_calendar <- function(.data,.date){
420417

421418
if(any(data_class %in% "tbl_lazy")){
422419

423-
out <- augment_calendar_dbi(.data = .data,.date = .date_var)
420+
out <- augment_standard_calendar_dbi(.data = .data,.date = .date_var)
424421

425422
return(out)
426423

@@ -429,7 +426,7 @@ augment_calendar <- function(.data,.date){
429426

430427
if(any(data_class %in% "tbl")){
431428

432-
out <- augment_calendar_tbl(.data = .data,.date = !!.date_var)
429+
out <- augment_standard_calendar_tbl(.data = .data,.date = !!.date_var)
433430

434431
return(out)
435432
}
@@ -460,6 +457,121 @@ closest_sunday_feb1 <- function(year) {
460457
}
461458

462459

460+
#' Create Non-Standard Month
461+
#'
462+
#' @param .data non-standard calendar table
463+
#' @param pattern '554','545' or '445'
464+
#'
465+
#' @returns DBI object
466+
#'
467+
#' @keywords internal
468+
create_ns_month <- function(.data,pattern){
469+
470+
471+
valid_colnames <- c("week_ns","year_ns")
472+
valid_pattern <- c("544","545","445")
473+
474+
475+
assertthat::assert_that(
476+
pattern %in% valid_pattern
477+
,msg = cli::cli_abort("Please select {.or {.val {valid_pattern}}}")
478+
)
479+
480+
481+
assertthat::assert_that(
482+
pattern %in% valid_pattern
483+
,msg = cli::cli_abort("Please ensure [.val week_ns] and [.val year_ns] are in the dataset")
484+
)
485+
486+
487+
if(pattern=="544"){
488+
489+
valid_cumulative_months <- cumsum(rep(c(5,4,4),4))
490+
491+
out <-
492+
.data |>
493+
dplyr::mutate(
494+
.by=year_ns
495+
,month_ns=dplyr::case_when(
496+
# either framing it in advance or somehow passing a arg to it
497+
week_ns<=!!valid_cumulative_months[[1]]~1
498+
,week_ns<=!!valid_cumulative_months[[2]]~2
499+
,week_ns<=!!valid_cumulative_months[[3]]~3
500+
,week_ns<=!!valid_cumulative_months[[4]]~4
501+
,week_ns<=!!valid_cumulative_months[[5]]~5
502+
,week_ns<=!!valid_cumulative_months[[6]]~6
503+
,week_ns<=!!valid_cumulative_months[[7]]~7
504+
,week_ns<=!!valid_cumulative_months[[8]]~8
505+
,week_ns<=!!valid_cumulative_months[[9]]~9
506+
,week_ns<=!!valid_cumulative_months[[10]]~10
507+
,week_ns<=!!valid_cumulative_months[[11]]~11
508+
,week_ns<=!!valid_cumulative_months[[12]]~12
509+
,.default=13
510+
)
511+
)
512+
}
513+
514+
if(pattern=="445"){
515+
516+
valid_cumulative_months <- cumsum(rep(c(4,4,5),4))
517+
518+
out <- .data |>
519+
dplyr::mutate(
520+
.by=year_ns
521+
,month_ns=dplyr::case_when(
522+
# either framing it in advance or somehow passing a arg to it
523+
week_ns<=!!valid_cumulative_months[[1]]~1
524+
,week_ns<=!!valid_cumulative_months[[2]]~2
525+
,week_ns<=!!valid_cumulative_months[[3]]~3
526+
,week_ns<=!!valid_cumulative_months[[4]]~4
527+
,week_ns<=!!valid_cumulative_months[[5]]~5
528+
,week_ns<=!!valid_cumulative_months[[6]]~6
529+
,week_ns<=!!valid_cumulative_months[[7]]~7
530+
,week_ns<=!!valid_cumulative_months[[8]]~8
531+
,week_ns<=!!valid_cumulative_months[[9]]~9
532+
,week_ns<=!!valid_cumulative_months[[10]]~10
533+
,week_ns<=!!valid_cumulative_months[[11]]~11
534+
,week_ns<=!!valid_cumulative_months[[12]]~12
535+
,.default=13
536+
)
537+
)
538+
}
539+
540+
541+
if(pattern=="454"){
542+
543+
valid_cumulative_months <- cumsum(rep(c(4,5,4),4))
544+
545+
out <- .data |>
546+
dplyr::mutate(
547+
.by=year_ns
548+
,month_ns=dplyr::case_when(
549+
# either framing it in advance or somehow passing a arg to it
550+
week_ns<=!!valid_cumulative_months[[1]]~1
551+
,week_ns<=!!valid_cumulative_months[[2]]~2
552+
,week_ns<=!!valid_cumulative_months[[3]]~3
553+
,week_ns<=!!valid_cumulative_months[[4]]~4
554+
,week_ns<=!!valid_cumulative_months[[5]]~5
555+
,week_ns<=!!valid_cumulative_months[[6]]~6
556+
,week_ns<=!!valid_cumulative_months[[7]]~7
557+
,week_ns<=!!valid_cumulative_months[[8]]~8
558+
,week_ns<=!!valid_cumulative_months[[9]]~9
559+
,week_ns<=!!valid_cumulative_months[[10]]~10
560+
,week_ns<=!!valid_cumulative_months[[11]]~11
561+
,week_ns<=!!valid_cumulative_months[[12]]~12
562+
,.default=13
563+
)
564+
)
565+
}
566+
567+
return(out)
568+
569+
570+
}
571+
572+
573+
574+
463575
utils::globalVariables(
464576
c(
465577
"desc",

man/closest_sunday_feb1.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_ns_month.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)