Skip to content

Instantly share code, notes, and snippets.

@czeildi
Created August 28, 2017 08:46
Show Gist options
  • Save czeildi/9d971f95df45ff4ab7c2e380d58f5ceb to your computer and use it in GitHub Desktop.
Save czeildi/9d971f95df45ff4ab7c2e380d58f5ceb to your computer and use it in GitHub Desktop.
how R handles week starts: Sunday vs Monday start
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
a_sunday <- as.Date("2017-08-27")
# floor_date rounds to Sunday:
weekdays(floor_date(Sys.Date(), "week"))
#> [1] "Sunday"
# week starts on Sunday:
lubridate::week(a_sunday - 1)
#> [1] 34
lubridate::week(a_sunday)
#> [1] 35
# isoweek starts on Monday:
lubridate::week(a_sunday)
#> [1] 35
lubridate::week(a_sunday + 1)
#> [1] 35
# format.Date: %U starts on Sunday
format(a_sunday - 1, "%U")
#> [1] "34"
format(a_sunday, "%U")
#> [1] "35"
# format.Date: %W starts on Monday
format(a_sunday, "%W")
#> [1] "34"
format(a_sunday + 1, "%W")
#> [1] "35"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment