I\'m looking for a function that will get year + week number + Week day, and return a date, for example:
I would like to input the 3 following
2015
Using strptime
:
strptime("2015Monday23", "%Y%A%U")
# [1] "2015-06-08"
Or more generally
strptime(paste0(2015, "Monday", 23), "%Y%A%U")
There are two caveats here:
The result depends on the current locale.
In my locale "German_Germany.1252"
(call Sys.getlocale("LC_TIME")
to check your locale), strptime("2015Monday23", "%Y%A%U")
returns NA
.
The results depends on the convention for numbering the weeks of a year.
There are 3 conventions R
is aware of: US, UK, and ISO 8601. See this answer for a detailed discussion. So, the convention to be used for conversion has to be specified.
If you are in a non-english locale, you can deal with English weekday names (or month names, likewise) by temporarily changing the current locale:
Sys.setlocale("LC_TIME", "US")
#> [1] "English_United States.1252"
strptime("2015Monday23", "%Y%A%U")
#> [1] "2015-06-08 CEST"
Sys.setlocale("LC_TIME")
#> [1] "German_Germany.1252"
The lubridate
package offers a more convenient way:
lubridate::parse_date_time("2015Monday23", "YAU", locale = "US")
#> [1] "2015-06-08 UTC"
As the weekday is given by its name, the US and UK conventions return the same result:
lubridate::parse_date_time("2015Monday23", "YAU", locale = "US")
#> [1] "2015-06-08 UTC"
lubridate::parse_date_time("2015Monday23", "YAW", locale = "UK")
#> [1] "2015-06-08 UTC"
Unfortunately, the format specifiers for the ISO 8601 convention are not accepted on input. So, we reverse the process and format the resulting date as week of the year in the different conventions which shows a differing result for ISO 8601.
format(as.Date("2015-06-08 UTC"), "%Y-%W-%u") # UK convention
#> [1] "2015-23-1"
format(as.Date("2015-06-08 UTC"), "%Y-%U-%w") # US convention
#> [1] "2015-23-1"
format(as.Date("2015-06-08 UTC"), "%G-%V-%u") # ISO 8601
#> [1] "2015-24-1"