Week numbers in R Language
To get the current ISO 8601 week number in R Language, use the following code snippet:
strftime(Sys.Date(), format = "%V")
* Ensure your system or application regional settings are set to follow ISO 8601 standards (weeks start on Monday) if the output varies.
Step-by-Step Instructions
- Use strftime(Sys.Date(), format = "%V") for today's ISO week number.
- For a date column, apply the same format string, or use lubridate::isoweek(dates).
- Convert the result with as.integer() if you need a number rather than a string.
Good to Know
Base R's %V is ISO 8601. If you use lubridate, isoweek() is ISO, but week() is NOT (it counts 7-day blocks from January 1). Mixing them is a common source of off-by-one bugs in reports.