【问题标题】:Using R, as.POSIXct() returning NAs even though it was working before使用 R,as.POSIXct() 返回 NA,即使它之前工作过
【发布时间】:2021-03-16 18:01:33
【问题描述】:

我正在使用 R。一个月前这对我有用,但现在当我尝试运行它时,我得到了 NA。我已尝试更改时区,但仍然收到 NA。我不知道为什么会遇到这个问题

as.POSIXct("12:46 29-Nov-18",format = "%H:%M %m/%d/%y")
as.POSIXct("12:46 29-Nov-18",format = "%H:%M %m/%d/%y",tz= "GMT")

【问题讨论】:

    标签: r date time posixct posixlt


    【解决方案1】:

    您的代码有几处错误,其中大部分已通过阅读?strptime 修复:

    • "%m" 是月份的 2 位数(0 填充)整数,而不是 Nov,请改用 %b
    • 字符串是破折号,而不是斜杠,所以%m/%d/%y 应该是%m-%d-%y
    • 字符串的日期在前,所以%d-%m-%y 的顺序应该是正确的。

    将这三个结合起来,我们就有了

    as.POSIXct("12:46 29-Nov-18",format = "%H:%M %d-%b-%y")
    # [1] "2018-11-29 12:46:00 EST"
    as.POSIXct("12:46 29-Nov-18",format = "%H:%M %d-%b-%y", tz = "GMT")
    # [1] "2018-11-29 12:46:00 GMT"
    

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 2012-01-21
      • 2012-11-23
      相关资源
      最近更新 更多