difftime() adds decimal numbers

删除回忆录丶 提交于 2019-12-01 01:22:20

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!