Week numbers in R

In R you can either use the lubridate package or the native format() function to work with ISO week numbers. See the lubridate documentation for more details.

How to get the week number from a date

To get the ISO week number (1-53), use lubridate::isoweek(date) or format(date, "%-V").

To get the corresponding four-digit year (e.g. 2022), use lubridate::isoyear(date) or format(date, "%G").

How to get the number of weeks in a year

To get the number of ISO weeks (i.e. the number of the last week) in a year, use lubridate::isoweek(sprintf("%d-12-28", year)) or format(as.Date(sprintf("%d-12-28", year)), "%-V").

year is an integer four-digit year.