I would like to create a vector from one date to another displayed as YYYYWW which increments by week. It is important that the weeks are displayed in ISO-8601 standard format, here is a link for reference to the ISO-8601: https://www.epochconverter.com/weeks/2021
To my knowledge, the neatest way to generate weeks in this format is by using lubridates isoyear and isoweek. For example:
from_date <- paste0(lubridate::isoyear("2020-01-01"),lubridate::isoweek("2021-01-01"))
> from_date
> "202053"
to_date <- paste0(lubridate::isoyear(Sys.Date()),lubridate::isoweek(Sys.Date()))
> to_date
> "20222"
How can I create an array between from_date and to_date with an increment of one week keeping the ISO-8601 standard?
I have tried something like
YearWeek <- seq(from_date, to_date, by = "weeks")
but seq takes date classes in this instance which forces me out of the ISO-8601 standard. For reference I would like my final result to look like this:
# Vector of YearWeek from 2021-01-01 to todays date incrmented by week
> YearWeek
> [1] 202053 202101 202102 202103 202104 202105 202106 202107 202108 202109 202110 202111 202112 202113 202114
[16] 202115 202116 202117 202118 202119 202120 202121 202122 202123 202124 202125 202126 202127 202128 202129
[31] 202130 202131 202132 202133 202134 202135 202136 202137 202138 202139 202140 202141 202142 202143 202144
[46] 202145 202146 202147 202148 202149 202150 202151 202152 202201
Please find below one possible solution to what you are looking for:
Reprex
library(lubridate)
# Data
vec_date <- seq(date("2020-01-01"), date("2022-01-12"), by = "weeks")
# Convert data into isodate and isoweek
vec_date_iso <- paste0(lubridate::isoyear(vec_date),lubridate::isoweek(vec_date))
vec_date_iso
#> [1] "20201" "20202" "20203" "20204" "20205" "20206" "20207" "20208"
#> [9] "20209" "202010" "202011" "202012" "202013" "202014" "202015" "202016"
#> [17] "202017" "202018" "202019" "202020" "202021" "202022" "202023" "202024"
#> [25] "202025" "202026" "202027" "202028" "202029" "202030" "202031" "202032"
#> [33] "202033" "202034" "202035" "202036" "202037" "202038" "202039" "202040"
#> [41] "202041" "202042" "202043" "202044" "202045" "202046" "202047" "202048"
#> [49] "202049" "202050" "202051" "202052" "202053" "20211" "20212" "20213"
#> [57] "20214" "20215" "20216" "20217" "20218" "20219" "202110" "202111"
#> [65] "202112" "202113" "202114" "202115" "202116" "202117" "202118" "202119"
#> [73] "202120" "202121" "202122" "202123" "202124" "202125" "202126" "202127"
#> [81] "202128" "202129" "202130" "202131" "202132" "202133" "202134" "202135"
#> [89] "202136" "202137" "202138" "202139" "202140" "202141" "202142" "202143"
#> [97] "202144" "202145" "202146" "202147" "202148" "202149" "202150" "202151"
#> [105] "202152" "20221" "20222"
Created on 2022-01-12 by the reprex package (v2.0.1)