[英]Consistent output of as.character for date objects in R
An external R library uses the as.character function to convert objects of different classes to strings. 外部R库使用as.character函数将不同类的对象转换为字符串。 That works fine except for my date objects (classes "POSIXlt" and "POSIXt"): Normally the output is like "2010-11-04 10:43:00" (which is perfect) but every time when time is 00:00:00 (midnight) the time component is ommited and only the date component is shown like "2010-11-04".
除我的日期对象(类“ POSIXlt”和“ POSIXt”)外,其他方法都正常运行:通常,输出类似于“ 2010-11-04 10:43:00”(完美),但是每次时间为00:00 :00(午夜),省略了时间部分,只有日期部分显示为“ 2010-11-04”。 But for further processing I need a consistent output format.
但是为了进一步处理,我需要一个一致的输出格式。 So the time component should be displayed in any case.
因此,无论如何都应显示时间部分。
I can't simply use the format function because the external library does the call. 我不能简单地使用format函数,因为外部库会进行调用。 So I thought that overwriting the as.character function for the classes "POSIXlt" and "POSIXt" could be a solution but I don't know how.
因此,我认为覆盖类“ POSIXlt”和“ POSIXt”的as.character函数可能是一种解决方案,但我不知道如何。 Other ideas are welcome :)
欢迎其他想法:)
You can overwrite the as.character
method for POSIXct
objects simply by creating your own. 您只需创建自己的对象即可覆盖
POSIXct
对象的as.character
方法。
as.character.POSIXct <- function(x, format="%Y-%m-%d %H:%M:%S", ...)
format(x, format=format, ...)
In this case though, there is no existing as.character.POSIXct
so you're not actually overwriting anything. 但是,在这种情况下,不存在
as.character.POSIXct
因此您实际上并未覆盖任何内容。 You are , however, overriding the default as.character.POSIXt
method, which is what would be called in the absence of a POSIXct
method. 但是,您将覆盖默认的
as.character.POSIXt
方法,该方法将在没有POSIXct
方法的情况下被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.