Working with week of the year can become very tricky. You may try to convert the dates using the ISOweek
package:
# create date strings in the format given by the OP
wd <- c("2016-50-4","2016-50-5","2016-50-6","2016-50-7", "2016-51-1", "2016-52-7")
# convert to "normal" dates
ISOweek::ISOweek2date(stringr::str_replace(wd, "-", "-W"))
The result
#[1] "2016-12-15" "2016-12-16" "2016-12-17" "2016-12-18" "2016-12-19" "2017-01-01"
is of class Date
.
Note that the ISO week-based date format is yyyy-Www-d
with a capital W
preceeding the week number. This is required to distinguish it from the standard month-based date format yyyy-mm-dd
.
So, in order to convert the date strings provided by the OP using ISOweek2date()
it is necessary to insert a W
after the first hyphen which is accomplished by replacing the first -
by -W
in each string.
Also note that ISO weeks start on Monday and the days of the week are numbered 1 to 7. The year which belongs to an ISO week may differ from the calendar year. This can be seen from the sample dates above where the week-based date 2016-W52-7
is converted to 2017-01-01
.
About the ISOweek
package
Back in 2011, the %G
, %g
, %u
, and %V
format specifications weren't available to strptime()
in the Windows version of R. This was annoying as I had to prepare weekly reports including week-on-week comparisons. I spent hours to find a solution for dealing with ISO weeks, ISO weekdays, and ISO years. Finally, I ended up creating the ISOweek
package and publishing it on CRAN. Today, the package still has its merits as the aforementioned formats are ignored on input (see ?strptime
for details).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…