我有一些来自 Excel 的数字日期数据:
> df <- data.frame(c(42613, 42612, 42614), c(42614.61, 42613.97, 42612.12))
> names(df) <- c("Closetime", "Opentime")
Both Closetime
and Opentime
are numeric
。我想保留小时/分钟/秒数据OpenTime
并将时间“00:00:00”添加到每个日期Closetime
:
> df$Closetime <- paste(as.Date(df$Closetime, origin = '1900-01-01'), c('00:00:00'))
很容易做到Closetime
,但是当我尝试使用lubridate::date_decimal
on Opentime
,一切都走向地狱。
> df$Opentime <- date_decimal(df$Opentime)
> df
Closetime Opentime
1 2016-09-02 00:00:00 42614-08-11 15:36:00
2 2016-09-01 00:00:00 42613-12-21 01:12:00
3 2016-09-03 00:00:00 42612-02-13 22:04:48
我怎样才能同时获得Opentime
and Closetime
具有相同的类型/格式?我最终希望能够获得每列中时间之间的差异(以小时为单位),以供参考。
如果你可以使用 POSIXct,你可以这样做
df$Opentime <- as.POSIXct( df$Opentime*24*60*60,
origin="1900-01-01",
tz="UTC")
推理:POSIXct 只是自起始点以来的秒数
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)