Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 带有日期时间(包括午夜)的as.POSIXct_R_Datetime_Posixct - Fatal编程技术网

R 带有日期时间(包括午夜)的as.POSIXct

R 带有日期时间(包括午夜)的as.POSIXct,r,datetime,posixct,R,Datetime,Posixct,我想将存储为字符的日期时间转换为日期时间对象。 但是,如果日期时间包含midnight,则生成的datetime对象将排除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.

我想将存储为字符的日期时间转换为日期时间对象。 但是,如果日期时间包含midnight,则生成的datetime对象将排除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]

example.dates好的,过一段时间我可以再次确认您的问题

对我来说,这看起来像是R中的一个bug。我建议你报告它

作为一种临时解决方法,您可以尝试覆盖以下
strtime
函数:

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
    }
}

strtime对于日期时间,我更喜欢使用
lubridate
包。它似乎也不会在这里引起问题:

example.dates <- c("2011-11-02 00:31:00","2011-11-02 00:00:00","2011-11-02 00:20:22")
library(lubridate)
ymd_hms(example.dates)
example.dateslubridate::as_datetime()更灵活,可以同时接受日期(解释为00:00:00)和日期时间

example.dates <- c("2011-11-02", "2011-11-02 00:31:00","2011-11-02 00:00:00","2011-11-02 00:20:22")
library(lubridate)

ymd_hms(example.dates)
#> Warning: 1 failed to parse.
#> [1] NA                        "2011-11-02 00:31:00 UTC"
#> [3] "2011-11-02 00:00:00 UTC" "2011-11-02 00:20:22 UTC"

as_datetime(example.dates)
#> [1] "2011-11-02 00:00:00 UTC" "2011-11-02 00:31:00 UTC"
#> [3] "2011-11-02 00:00:00 UTC" "2011-11-02 00:20:22 UTC"
example.dates警告:1解析失败。
#>[1]NA“2011-11-02 00:31:00 UTC”
#>[3]“2011-11-02 00:00:00 UTC”“2011-11-02 00:20:22 UTC”
as_datetime(例如日期)
#>[1]“2011-11-02 00:00:00 UTC”“2011-11-02 00:31:00 UTC”
#>[3]“2011-11-02 00:00:00 UTC”“2011-11-02 00:20:22 UTC”

意识到这是一个老问题,但我也遇到了同样的问题,并找到了解决方案:

基本上,您所需要做的就是按如下方式应用格式。OP的代码需要在POSIXct函数调用之后包含格式化调用

posix.dates <- format(as.POSIXct(example.dates, tz="GMT"), format="%Y-%m-%d %H:%M:%S")

posix.dates时间信息在那里,只是不显示。我有点惊讶,这会导致以后出现错误。也许问题出在你以后的功能上。你能给我们看看那个密码吗?(作为可用的证明,请参见
格式(posix.dates,%Y-%m-%d%m:%H')
谢谢@Thilo。我想使用R包“RNCEP”中的函数“NCEP.interp”。查看该函数的源代码,错误来自以下代码行,返回NA:strtime(posix.dates[2],%Y-%m-%d%H:%m:%S],“UTC”)考虑到我已经在使用datetime对象,这应该是不必要的,但函数就是这样编写的。我担心您的错误在其他地方。如果我运行
strTime(posix.dates,“%Y-%m-%d%H:%m:%S”,“UTC”)
,结果很好,符合预期。实际的错误消息是什么?如果在单个值而不是整个向量上运行它:strtime(posix.dates[2],%Y-%m-%d%H:%m:%s“,'UTC”)此处没有错误消息,但返回“NA”,在以后的函数中使用时会出现错误。我建议不要在
POSIXct
值上使用
strtime
。如果要转换为
POSIXlt
as.POSIXlt
更好。如果要使用
strtime
,请首先转换为
character
您自己是这样的:
strtime(格式为posix.dates[2],%Y-%m-%d%H:%m:%S”),%Y-%m-%d%H:%m:%S”,“UTC”)
谢谢Thilo和@GSee。我不能重写strTime,也不能避免使用它,除非我进入调用函数的包“RNCEP”——也就是说,我必须重写包源代码并重新编译。在这个特定的情况下,运行“NCEP.interp”函数,我想到的唯一简单的解决方法是不转换我使用的字符datetime首先,让这个包使用strtime来完成这个任务——这似乎是可行的。谢谢,但不幸的是,这会带来同样的问题(请参阅上面添加的两行代码)。