【问题标题】:Formatting as posixct if information on day of the month is missing如果缺少当月日期的信息,则格式化为 posixct
【发布时间】:2019-06-30 14:25:46
【问题描述】:

我在使用 as.POSIXct() 时遇到了一些问题。

以向量为例: Dates <- c("2015-01 00","2015-01 01","2015-01 02","2015-01 03","2015-01 04","2015-01 05")

如您所见,我有关于年、月和小时的信息(但没有月中的某一天)。 我不知道我做错了什么,但使用as.POSIXct(Dates,"%Y-%m %H",tz="GMT")只会创建一堆 NA。

任何解决方案都非常受欢迎。

【问题讨论】:

    标签: r date format posixct


    【解决方案1】:

    我们可以插入一天,例如每月的第一天(Date 也需要一天)使用sub,然后使用as.POSIXct

    as.POSIXct(sub("\\s", "-01 ", Dates), format = "%Y-%m-%d %H")
    #[1] "2015-01-01 00:00:00 EST" "2015-01-01 01:00:00 EST" "2015-01-01 02:00:00 EST" "2015-01-01 03:00:00 EST" "2015-01-01 04:00:00 EST"
    #[6] "2015-01-01 05:00:00 EST"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2017-12-24
      • 1970-01-01
      • 2015-12-29
      • 2021-04-02
      相关资源
      最近更新 更多