Search code examples
rdifftime

difftime() adds decimal numbers


I am trying to get the number of days from a specific date using difftime(). If I use the November date (10.11.14) it works fine, if I use the September date (10.09.14) it adds .0417 to the date. Any idea how I can solve this?

head(dummydat)
reihe nummer bluh_datum
1     1      1   07.03.15
2     1      2   11.03.15
3     1      3   09.03.15
4     1      4       <NA>
5     1      5       <NA>
6     1      6   07.03.15

dummydat <- cbind(dummydat,"days"=as.numeric(difftime(strptime(dummydat$bluh_datum, format="%d.%m.%y"),strptime("10.11.14", format="%d.%m.%y"), units="days")))
> head(dummydat)
  reihe nummer bluh_datum days
1     1      1   07.03.15  117
2     1      2   11.03.15  121
3     1      3   09.03.15  119
4     1      4       <NA>   NA
5     1      5       <NA>   NA
6     1      6   07.03.15  117
> dummydat <- cbind(dummydat,"days"=as.numeric(difftime(strptime(dummydat$bluh_datum, format="%d.%m.%y"),strptime("10.09.14", format="%d.%m.%y"), units="days")))
> head(dummydat)
  reihe nummer bluh_datum days     days
1     1      1   07.03.15  117 178.0417
2     1      2   11.03.15  121 182.0417
3     1      3   09.03.15  119 180.0417
4     1      4       <NA>   NA       NA
5     1      5       <NA>   NA       NA
6     1      6   07.03.15  117 178.0417

Solution

  • instead of strptime you can use as.Date like this

    difftime(as.Date(dummydat$bluh_datum, format = "%d.%m.%y"), as.Date("10.11.14", format = "%d.%m.%y"), units = "days")
    # Time differences in days
    # [1] 117 121 119  NA  NA 117
    difftime(as.Date(dummydat$bluh_datum, format = "%d.%m.%y"), as.Date("10.09.14", format = "%d.%m.%y"),  units = "days" )
    # Time differences in days
    # [1] 178 182 180  NA  NA 178
    

    or you have to specify the time zone tz="GMT" like this

    difftime(strptime(dummydat$bluh_datum, format="%d.%m.%y", tz = "GMT"), strptime("10.09.14", format="%d.%m.%y", tz = "GMT"), units="days")
    # Time differences in days
    # [1] 178 182 180  NA  NA 178
    difftime(strptime(dummydat$bluh_datum, format="%d.%m.%y", tz = "GMT"),strptime("10.11.14", format="%d.%m.%y", tz = "GMT"), units="days")
    # Time differences in days
    # [1] 117 121 119  NA  NA 117
    

    if you do not specify the time zone look what happens

    strptime(dummydat$bluh_datum, format="%d.%m.%y")
    # [1] "2015-03-07 CET" "2015-03-11 CET" "2015-03-09 CET" NA               NA               "2015-03-07 CET"
    strptime("10.09.14", format="%d.%m.%y")
    ## [1] "2014-09-10 CEST"
    

    the time zones will be different between dates.