tidyverts / tsibble

Tidy Temporal Data Frames and Tools
https://tsibble.tidyverts.org
GNU General Public License v3.0
528 stars 50 forks source link

Week 53 in 2021 #262

Closed KoderKow closed 2 years ago

KoderKow commented 2 years ago

Hello! I am working with some data that uses week 53 in 2021, which corresponds to "2021-12-31". I am wanting to use a year week format and I have not come across week 53 before. Lubridate will return a 53, however, the yearweek() function will return week 52. Is this expected? I am grouping by year week and want to make sure I am correctly using the weeks. If there is a recommended approach I am happy to adjust my code to do so.

Thanks!

## Sample date that is in week 53
my_date <- lubridate::ymd("2021-12-31")

## Returns 53
lubridate::week(my_date)
#> [1] 53

## Returns 52
tsibble::yearweek(my_date)
#> <yearweek[1]>
#> [1] "2021 W52"
#> # Week starts on: Monday

## Errors
tsibble::yearweek("2021 Week 53")
#> Error: Year 2021 can't be 53 weeks.

Created on 2021-09-30 by the reprex package (v2.0.0)

current session info ```r - Session info ------------------------------------------------------------------------------------------------- setting value version R version 4.0.2 (2020-06-22) os Windows 10 x64 system x86_64, mingw32 ui RStudio language (EN) collate English_United States.1252 ctype English_United States.1252 tz UTC date 2021-09-30 - Packages ----------------------------------------------------------------------------------------------------- package * version date lib source askpass 1.1 2019-01-13 [1] RSPM (R 4.0.3) cachem 1.0.6 2021-08-19 [1] RSPM (R 4.0.5) callr 3.7.0 2021-04-20 [1] RSPM (R 4.0.4) cli 3.0.1 2021-07-17 [1] RSPM (R 4.0.5) clipr 0.7.1 2020-10-08 [1] RSPM (R 4.0.3) codetools 0.2-16 2018-12-24 [2] CRAN (R 4.0.2) crayon 1.4.1 2021-02-08 [1] RSPM (R 4.0.3) credentials 1.3.1 2021-07-25 [1] RSPM (R 4.0.5) curl 4.3.2 2021-06-23 [1] RSPM (R 4.0.5) data.table 1.14.0 2021-02-21 [1] RSPM (R 4.0.3) desc 1.3.0 2021-03-05 [1] RSPM (R 4.0.3) details * 0.2.2 2021-09-30 [1] Github (yonicd/details@d50e8ef) devtools * 2.4.2 2021-06-07 [1] RSPM (R 4.0.5) digest 0.6.27 2020-10-24 [1] RSPM (R 4.0.3) ellipsis 0.3.2 2021-04-29 [1] RSPM (R 4.0.4) evaluate 0.14 2019-05-28 [1] RSPM (R 4.0.3) fansi 0.5.0 2021-05-25 [1] RSPM (R 4.0.5) fastmap 1.1.0 2021-01-25 [1] RSPM (R 4.0.3) fs 1.5.0 2020-07-31 [1] RSPM (R 4.0.3) gert 1.3.2 2021-08-16 [1] RSPM (R 4.0.5) glue 1.4.2 2020-08-27 [1] RSPM (R 4.0.3) hms 1.1.0 2021-05-17 [1] RSPM (R 4.0.4) htmltools 0.5.1.1 2021-01-22 [1] RSPM (R 4.0.3) httr 1.4.2 2020-07-20 [1] RSPM (R 4.0.3) igraph 1.2.6 2020-10-06 [1] RSPM (R 4.0.3) jsonlite 1.7.2 2020-12-09 [1] RSPM (R 4.0.3) knitr 1.33 2021-04-24 [1] RSPM (R 4.0.4) lifecycle 1.0.0 2021-02-15 [1] RSPM (R 4.0.3) magrittr 2.0.1 2020-11-17 [1] RSPM (R 4.0.3) matahari 0.1.3 2020-02-06 [1] RSPM (R 4.0.0) memoise 2.0.0 2021-01-26 [1] RSPM (R 4.0.3) openssl 1.4.4 2021-04-30 [1] RSPM (R 4.0.4) pillar 1.6.2 2021-07-29 [1] RSPM (R 4.0.5) pkgbuild 1.2.0 2020-12-15 [1] RSPM (R 4.0.3) pkgconfig 2.0.3 2019-09-22 [1] RSPM (R 4.0.3) pkgload 1.2.1 2021-04-06 [1] RSPM (R 4.0.4) png 0.1-7 2013-12-03 [1] RSPM (R 4.0.3) prettyunits 1.1.1 2020-01-24 [1] RSPM (R 4.0.3) processx 3.5.1 2021-04-04 [1] RSPM (R 4.0.4) prompt 1.0.1 2021-07-01 [1] Github (gaborcsardi/prompt@fc2ac94) ps 1.6.0 2021-02-28 [1] RSPM (R 4.0.3) purrr 0.3.4 2020-04-17 [1] RSPM (R 4.0.3) R6 2.5.1 2021-08-19 [1] RSPM (R 4.0.5) readr 2.0.1 2021-08-10 [1] RSPM (R 4.0.5) remotes 2.3.0 2021-04-01 [1] RSPM (R 4.0.4) reprex * 2.0.0 2021-04-02 [1] RSPM (R 4.0.4) rlang 0.4.11 2021-04-30 [1] RSPM (R 4.0.4) rmarkdown 2.11 2021-09-14 [1] RSPM (R 4.0.5) rprojroot 2.0.2 2020-11-15 [1] RSPM (R 4.0.3) rsthemes 0.2.1.9000 2021-09-23 [1] Github (gadenbuie/rsthemes@19299e5) rstudioapi 0.13 2020-11-12 [1] RSPM (R 4.0.3) sessioninfo 1.1.1 2018-11-05 [1] RSPM (R 4.0.3) sys 3.4 2020-07-23 [1] RSPM (R 4.0.3) tarchetypes * 0.3.0 2021-08-04 [1] RSPM (R 4.0.5) targets * 0.6.0 2021-07-21 [1] RSPM (R 4.0.5) testthat 3.0.2 2021-02-14 [1] RSPM (R 4.0.3) tibble 3.1.3 2021-07-23 [1] RSPM (R 4.0.5) tidyselect 1.1.1 2021-04-30 [1] RSPM (R 4.0.4) tzdb 0.1.2 2021-07-20 [1] RSPM (R 4.0.5) usethis * 2.0.1 2021-02-10 [1] RSPM (R 4.0.3) utf8 1.2.2 2021-07-24 [1] RSPM (R 4.0.5) vctrs 0.3.8 2021-04-29 [1] RSPM (R 4.0.4) withr 2.4.2 2021-04-18 [1] RSPM (R 4.0.4) xfun 0.25 2021-08-06 [1] RSPM (R 4.0.5) xml2 1.3.2 2020-04-23 [1] RSPM (R 4.0.3) yaml 2.2.1 2020-02-01 [1] RSPM (R 4.0.3) [1] C:/Users/-----/Documents/R/win-library/4.0 [2] C:/Program Files/R/R-4.0.2/library ```


earowang commented 2 years ago

lubridate::week() and tsibble::yearweek() both are right. week() gives the number of weeks from Jan 1st of the year, whereas yearweek() gives the number of ISO weeks.

KoderKow commented 2 years ago

Ah got it, I was unaware that ISO weeks will always have 52! Good to know! Thank you, I will close this issue. Appreciate the quick response and explanation!