as.POSIXct,日期时间包括午夜 [英] as.POSIXct with datetimes including midnight
问题描述
我想将存储为字符的日期时间转换为日期时间对象。
但是,如果日期时间包括午夜,则生成的datetime对象将排除时间部分,该时间部分将在以后的函数中使用时引发错误(此处不需要-但是该函数可提取指定位置和日期的天气数据,
I am wanting to convert date-times stored as characters to date-time objects. However if a date time includes midnight then the resulting datetime object excludes the time component, which then throws an error when used in a later function (not needed here - but a function that extracts weather data for specified location and date-time).
示例代码:
example.dates <- c("2011-11-02 00:31:00","2011-11-02 00:00:00","2011-11-02 00:20:22")
posix.dates <- as.POSIXct(example.dates, tz="GMT", format="%Y-%m-%d %H:%M:%S")
posix.dates
posix.dates[2]
NB时间仅在单独调用包含午夜的datetime时被排除(原子向量)。
NB times is only excluded when the datetime containing midnight is called on it's own (atomic vector).
是否可以将时间数据保留为午夜时间?可以建议其他功能吗?
Is there a way of retaining the time data for midnight times? Can you suggest an alternative function?
推荐答案
好的,过一段时间后,我可以再次确认您的问题。
Okay, after some time I can reconfirm your problem.
对我来说,这似乎是R中的错误。建议您在 https://bugs.r-project.org/bugzilla3/ 。
For me this looks like a bug in R. I would suggest you to report it on https://bugs.r-project.org/bugzilla3/.
作为一种临时的解决方法,您可以尝试是否有助于覆盖 strptime
函数,如下所示:
As a temporary workaround, you could try if it helps to overwrite the strptime
function like this:
strptime <- function (x, format, tz = "")
{
if ("POSIXct" %in% class(x)) {
x
} else {
y <- .Internal(strptime(as.character(x), format, tz))
names(y$year) <- names(x)
y
}
}
这篇关于as.POSIXct,日期时间包括午夜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!