Search code examples
rdateisoposixct

The week starts at 2 o'clock for the ISO 8601 definition in strptime


With this dummy dataframe:

library(tibble)
library(dplyr)
tibble(Date = as.POSIXct(c(1601848800, 1601852400, 1601856000, 
        1601859600, 1601863200), origin = "1970-01-01")) %>%
          mutate(ISOweek = format(as.Date(.$Date), "%V"))

I get this result which baffles me:

    Date                ISOweek
  <dttm>              <chr>  
1 2020-10-05 00:00:00 40     
2 2020-10-05 01:00:00 40     
3 2020-10-05 02:00:00 41     
4 2020-10-05 03:00:00 41     
5 2020-10-05 04:00:00 41

I am getting the Date variable from a csv file, so maybe I am converting from character to POSIXct in an unproper manner.


Solution

  • as.POSIXct uses your local timezone and as.Date uses UTC timezone. If you specify the timezone in as.POSIXct the output will be clear to you. Also adding a date column should help clarify the result.

    library(dplyr)
    
    tibble(Date = as.POSIXct(c(1601848800, 1601852400, 1601856000, 1601859600, 
                               1601863200), origin = "1970-01-01", tz = 'UTC')) %>%
        mutate(date = as.Date(Date), 
               ISOweek = format(date, "%V")) 
    
    
    #  Date                date       ISOweek
    #  <dttm>              <date>     <chr>  
    #1 2020-10-04 22:00:00 2020-10-04 40     
    #2 2020-10-04 23:00:00 2020-10-04 40     
    #3 2020-10-05 00:00:00 2020-10-05 41     
    #4 2020-10-05 01:00:00 2020-10-05 41     
    #5 2020-10-05 02:00:00 2020-10-05 41