How do I create a sequence of year-week? For example, if the starting year is 2019 and the ending year is 2020, the sequence will be 201901, 201902, 201903, ..., 202053.
library(lubridate)
seq(from = mdy("1/1/2019"), to = mdy("1/1/2020"), by = "week")
Thanks for the reply. Unfortunately, this is not what I wanted. Your code produces dates, but I am looking for year-week.
I am not aware of a data type for week, only date and datetime types. I think it's most common to use date type for day, week, month, quarter frequencies. At least you can still plot on an axis and do date difference / interval calculations.
There is no base R "year-week" type but if you can work with character or numeric type data, then this should work
format(seq(from = as.Date("2019-01-01"), to = as.Date("2020-12-31"), by = "week"), "%Y%V")
#> [1] "201901" "201902" "201903" "201904" "201905" "201906" "201907" "201908"
#> [9] "201909" "201910" "201911" "201912" "201913" "201914" "201915" "201916"
#> [17] "201917" "201918" "201919" "201920" "201921" "201922" "201923" "201924"
#> [25] "201925" "201926" "201927" "201928" "201929" "201930" "201931" "201932"
#> [33] "201933" "201934" "201935" "201936" "201937" "201938" "201939" "201940"
#> [41] "201941" "201942" "201943" "201944" "201945" "201946" "201947" "201948"
#> [49] "201949" "201950" "201951" "201952" "201901" "202002" "202003" "202004"
#> [57] "202005" "202006" "202007" "202008" "202009" "202010" "202011" "202012"
#> [65] "202013" "202014" "202015" "202016" "202017" "202018" "202019" "202020"
#> [73] "202021" "202022" "202023" "202024" "202025" "202026" "202027" "202028"
#> [81] "202029" "202030" "202031" "202032" "202033" "202034" "202035" "202036"
#> [89] "202037" "202038" "202039" "202040" "202041" "202042" "202043" "202044"
#> [97] "202045" "202046" "202047" "202048" "202049" "202050" "202051" "202052"
#> [105] "202053"
Created on 2022-02-24 by the reprex package (v2.0.1)
@andresrcs many thanks! Character/numeric type data is fine for me.
-
Could you please explain what
"%Y%V"
means? In addition, when I apply your code for 2016 data, I wonder why the sequence starts at 2016053. Just curious -
How can I get a sequence of ISO weeks? Google search reveals that there were 52 weeks in 2016.
format(seq(from = as.Date("2016-01-01"), to = as.Date("2016-12-31"),
by = "week"), "%Y%V")
#> [1] "201653" "201601" "201602" "201603" "201604" "201605" "201606" "201607"
#> [9] "201608" "201609" "201610" "201611" "201612" "201613" "201614" "201615"
#> [17] "201616" "201617" "201618" "201619" "201620" "201621" "201622" "201623"
#> [25] "201624" "201625" "201626" "201627" "201628" "201629" "201630" "201631"
#> [33] "201632" "201633" "201634" "201635" "201636" "201637" "201638" "201639"
#> [41] "201640" "201641" "201642" "201643" "201644" "201645" "201646" "201647"
#> [49] "201648" "201649" "201650" "201651" "201652"
Is just a date format specification, %Y means 4 digit year and %V is the ISO week number, although that behavior is un intuitive for me too.
Here is a nice explanation.
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.