Skip to content

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

Closed

Description

@CameronBieganek

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")
week(x)

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:

function(x)
yday(x) %/% 7 + 1

to this:

function(x)
(yday(x) - 1) %/% 7 + 1

Activity

  1. IamGianluca commented on Sep 2, 2014

    @IamGianluca

    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
    
  2. CameronBieganek commented on Sep 5, 2014

    @CameronBieganek
    Author

    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.

  3. vspinu commented on Dec 14, 2014

    @vspinu
    Member

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions