Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week() function returns a 6 day week at the beginning of a year #251

CameronBieganek opened this issue Jul 30, 2014 · 3 comments


Copy link

There is a bug in the week() function which leads to the first week of the year being only 6 days long. For example, if you type

x <- seq(from = ymd("2014-01-01"), to = ymd("2014-01-10"), by = "days")

then you'll get 1 1 1 1 1 1 2 2 2 2. The code should be changed from it's current state, which is this:

yday(x) %/% 7 + 1

to this:

(yday(x) - 1) %/% 7 + 1
Copy link

I confirm there is an issue with the week() function. The week starts on Tuesday and ends on Monday.

# The 1st of Sep is a Monday
> week(ymd("2014-09-01"))
[1] 35
# The 2nd of Sep is a Tuesday
> week(ymd("2014-09-02"))
[1] 36
# The 7th of Sep is a Sunday
> week(ymd("2014-09-07"))
[1] 36
# The 8th of Sep is a Monday
> week(ymd("2014-09-08"))
[1] 36
# The 9th of Sep is a Tuesday 
> week(ymd("2014-09-09"))
[1] 37

Copy link

I don't think that the behavior mentioned by IamGianluca is actually a bug. The week documentation says

"Weeks is the number of complete seven day periods that have occurred between the date and January 1st, plus one."

Thus, the weeks returned by week won't normally run Sunday through Saturday (as the wday function does), unless the first day of the year is a Sunday. So since 2014 starts on a Wednesday, the first week (as returned by the week function) should run from Wednesday, January 1 through Tuesday, January 7, and the second week should run from Wednesday, January 8 through Tuesday, January 14, etc. However, the bug I mentioned is causing the first week to end on Monday, January 6 rather than Tuesday, January 7. Then subsequent weeks run from Tuesday through Monday.

@vspinu vspinu closed this as completed in 4429eb6 Dec 14, 2014
Copy link

vspinu commented Dec 14, 2014

@clbieganek you are right. This was a bug, and yes the behavior mentioned by @IamGianluca is as expected. week computes the number of 7 period intervals since the begining of the year including the current week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

3 participants