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
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.
Try as.integer
instead of as.numeric
if you don't want the decimals.
c(as.numeric(difftime("2015-12-02", "2014-12-02")), as.numeric(difftime("2015-12-02", "2014-11-01")))
# [1] 365.0000 396.0417
c(as.integer(difftime("2015-12-02", "2014-12-02")), as.integer(difftime("2015-12-02", "2014-11-01")))
# [1] 365 396
来源:https://stackoverflow.com/questions/29558742/difftime-adds-decimal-numbers