我正在研究使用新的data.table
日期和时间格式。由于ITime
在底层是以整数形式存储的(自午夜以来的秒数):
x <- as.ITime( "10:25:00" )
i <- as.integer( x )
i
[1] 37500
看起来将整数(假设小于24*60*60)转换为 ITime
应该很容易,但我找不到如何实现:
as.ITime( i )
Error in as.POSIXlt.numeric(x, ...) : 'origin' must be supplied
as( i, "ITime" )
Error in as(i, "ITime") :
no method or default for coercing “integer” to “ITime”
提供一个起始时间(我猜是通过 as.POSIX...
传递)确实有所帮助,但即使这样,只有在您将 tz
指定为 "UTC" 时才能得到正确的值(至少对于我的语言环境而言):
as.ITime( i, origin = "1970-01-01 00:00:00" )
[1] "20:25:00"
as.ITime( i, origin = "1970-01-01 00:00:00", tz = "UTC" )
[1] "10:25:00"
# note that the date seems necessary but irrelevant here
as.ITime( i, origin = "2000-01-01 00:00:00", tz = "UTC" )
[1] "10:25:00"
我所说的是类似于我有时与
chron::times
一起使用的东西:x <- chron::times( "10:25:00" )
n <- as.numeric( x )
n
[1] 0.4340278
chron::times( n )
[1] 10:25:00
是否有一个类似的方法(除了使用整数而不是数字/双精度/浮点数)可用于 ITime
类?
请注意,我知道与 IDate
、Date
等相同的“来源要求”问题,但对我来说这更有意义,因为将 origin="1970-01-01"
作为时间的起点与使用“午夜”作为起点相比要少得多(我使用一些数据源对哪个日期应该被称为“起点”有不同的想法)。
setattr
实现这个? - David Arenburgsetattr(1:10, "class", "ITime")
基本上就是我想要实现的。如果您有其他方法,我很乐意听取。 - rosscova