Home > database >  Get a date from isoyear, isoweek, and wday values
Get a date from isoyear, isoweek, and wday values

Time:01-16

Is there any function that provides a date from isoyear, isoweek, and wday values?

For example:

isoyear_val <- 2020
isoweek_val <- 1
wday_val <- 2

x <- magic_function(isoyear_val, isoweek_val, wday_val)
# x is "2019-12-30"

I know that, using Lubridate, dates can be set for functions such as week():

x <- ymd("2012-03-26")
week(x) <- 30
# x is "2012-07-23"

But I have not found anything similar for ISO values.

CodePudding user response:

We may use make_yearweek from tsibble

library(tsibble)
as.Date(make_yearweek(year = isoyear_val, week = isoweek_val))
[1] "2019-12-30"

Not clear about the 'wday_val', if it meant to be the week_start

as.Date(make_yearweek(year  = isoyear_val, week = isoweek_val, 
      week_start = wday_val))
[1] "2019-12-31"

CodePudding user response:

There is a package by @uwe called ISOweek you could try the following. The package has helper functions to deal with week of the year and weekday according to ISO 8601.

library(ISOweek)

ISOweek2date(
  sprintf(
    "%d-Wd-%d",
    isoyear_val,
    isoweek_val,
    wday_val
  )
)

Output

[1] "2019-12-31"
  •  Tags:  
  • Related