Skip to content

Commit 8393bdd

Browse files
committed
interim 554 calendar submission
1 parent 28af5aa commit 8393bdd

File tree

3 files changed

+115
-18
lines changed

3 files changed

+115
-18
lines changed

554.R

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,103 @@
11
library(tidyverse)
22
library(contoso)
3-
devtools::document()
4-
53
devtools::load_all()
64

7-
8-
95
## create 5-5-4 calendar
106

117
x <- contoso::sales |> fpaR::mtd(order_date,revenue,calendar_type = "standard")
128

139

1410
con <- dbplyr::remote_con(x@datum@data)
1511

16-
# complete calendar with all attributes
1712

18-
new_cal <- fpaR:::seq_date_sql(start_date = "2025-01-01",end_date = "2025-12-31",time_unit = "day",con =con ) |>
13+
## find beginng date indicator
14+
15+
min_year <- year(x@datum@min_date)
16+
17+
start_year <- closest_sunday_feb1(min_year)
18+
19+
20+
new_cal <- fpaR:::seq_date_sql(start_date = start_year,end_date=x@datum@max_date,time_unit = "day",con =con ) |>
1921
augment_calendar(.date = date)
2022

2123

24+
2225
## next step add in new year indicator
2326
new_cal |>
2427
dplyr::mutate(
25-
new_year_date_indicator=if_else(month_of_year==2&day_of_year==4,1,0)
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+
)
2642
,.before=1
2743
)
2844

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)
54+
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
58+
59+
# Total weeks to generate
60+
total_weeks <- length(weeks_per_year) * num_years
61+
62+
# Generate week numbers
63+
week_number <- 1:total_weeks
64+
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
91+
) |>
92+
mutate(
93+
date=as.Date(date)
94+
)
95+
}
96+
97+
# Example usage: daily calendar starting Feb 2, 2020, for 1 year
98+
generate_5_5_4_daily("2020-02-02", num_years = 1)
99+
100+
29101
## next step add in new year indicator
30102

31103

R/utils-misc.R

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,12 @@ augment_calendar <- function(.data,.date){
418418
,msg = ".data must be regular tibble or DBI lazy object"
419419
)
420420

421-
422-
423421
if(any(data_class %in% "tbl_lazy")){
424422

425423
out <- augment_calendar_dbi(.data = .data,.date = .date_var)
426424

427425
return(out)
428426

429-
430427
}
431428

432429

@@ -435,14 +432,32 @@ augment_calendar <- function(.data,.date){
435432
out <- augment_calendar_tbl(.data = .data,.date = !!.date_var)
436433

437434
return(out)
435+
}
438436

437+
}
439438

440-
}
441439

440+
#' Finds closet sunday to February 1st
441+
#'
442+
#' @param year year of the date
443+
#'
444+
#' @returns character vector
445+
#' @keywords internal
446+
closest_sunday_feb1 <- function(year) {
442447

448+
# Create a Date object for February 1st of the given year
449+
feb1 <- as.Date(paste0(year, "-02-01"))
443450

444-
}
451+
# Get the day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
452+
wday <- as.integer(format(feb1, "%w"))
453+
454+
# Calculate offset to the nearest Sunday
455+
# If wday <= 3, the closest Sunday is before; else it's after
456+
offset <- ifelse(wday <= 3, -wday, 7 - wday)
445457

458+
# Return the closest Sunday
459+
feb1 + offset
460+
}
446461

447462

448463
utils::globalVariables(

renv.lock

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@
934934
},
935935
"contoso": {
936936
"Package": "contoso",
937-
"Version": "1.2.0",
938-
"Source": "Repository",
937+
"Version": "1.2.2",
938+
"Source": "GitHub",
939939
"Type": "Package",
940940
"Title": "Dataset of the 'Contoso' Company",
941941
"Authors@R": "c( person( \"Alejandro\", \"Hagan\", email = \"alejandro.hagan@outlook.com\", role = c(\"aut\", \"cre\") ) )",
@@ -945,24 +945,34 @@
945945
"DBI",
946946
"dplyr",
947947
"cli",
948-
"duckdb (>= 1.4.0)"
948+
"duckdb (>= 1.4.0)",
949+
"assertthat",
950+
"cli"
949951
],
950952
"Suggests": [
951-
"testthat (>= 3.0.0)"
953+
"testthat (>= 3.0.0)",
954+
"dbplyr"
952955
],
953956
"Encoding": "UTF-8",
954957
"LazyData": "true",
955958
"RoxygenNote": "7.3.3",
959+
"Roxygen": "list(markdown = TRUE)",
956960
"Depends": [
957961
"R (>= 4.1.0)"
958962
],
959963
"URL": "https://usrbinr.github.io/contoso/, https://github.com/usrbinr/contoso",
960964
"Config/testthat/edition": "3",
961965
"BugReports": "https://github.com/usrbinr/contoso/issues",
966+
"RemoteType": "github",
967+
"RemoteHost": "api.github.com",
968+
"RemoteRepo": "contoso",
969+
"RemoteUsername": "usrbinr",
970+
"RemotePkgRef": "usrbinr/contoso",
971+
"RemoteRef": "HEAD",
972+
"RemoteSha": "5157e022b4d5871cce95627d879a4a40da1d2b9f",
962973
"NeedsCompilation": "no",
963974
"Author": "Alejandro Hagan [aut, cre]",
964-
"Maintainer": "Alejandro Hagan <alejandro.hagan@outlook.com>",
965-
"Repository": "CRAN"
975+
"Maintainer": "Alejandro Hagan <alejandro.hagan@outlook.com>"
966976
},
967977
"cpp11": {
968978
"Package": "cpp11",

0 commit comments

Comments
 (0)