Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange R strptime behavior for week of year

Someone please explain this:

strptime(c("2010-01", "2010-02"), format="%Y-%W")
[1] "2010-05-21" "2010-05-21"

Why May 21st? I'm expecting the first and second weeks of the year (01) and (02) to be in January... %W is week of the year, isn't it?

ISOweek2date in the ISOweek package works:

library(ISOweek)
ISOweek2date(c("2010-W01-1", "2010-W02-1"))
[1] "2010-01-04" "2010-01-11"
like image 408
stackoverflax Avatar asked Dec 13 '25 17:12

stackoverflax


1 Answers

To get the same results as of ISOweek2date, you should give strptime the same level of details in the time string and format as you did for ISOweek2date.

So adding a "-1" for first day in the week in the time string and -%w in the format string gives you the same result as ISOweek2date:

strptime(c("2010-01-1", "2010-02-1"), format="%Y-%W-%w")
#[1] "2010-01-04" "2010-01-11"

As mentioned above, without enough information you get the current date

like image 173
leo Avatar answered Dec 15 '25 11:12

leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!