【问题标题】:Converting flux column timedate to posixct for hour rounding: error convert class to POSIXct将通量列 timedate 转换为 posixct 以进行小时舍入:错误将类转换为 POSIXct
【发布时间】:2021-07-07 10:14:54
【问题描述】:

我需要将通量数据中的日期时间列四舍五入到 R 中最接近的小时,但是当我使用 asPOSIXct 时,我得到了响应

as.POSIXct.default(flux_dt10, format = "%Y-%m-%dT%H:%M:%OS") 中的错误: 不知道如何将“flux_dt10”转换为“POSIXct”类

数据集是一列值,例如:

flux_dt10 <- "01APR2019:11:51:42"

我试过了:

as.POSIXct(flux_dt10,format="%Y-%m-%dT%H:%M:%OS")

但给出的响应不知道如何将数据转换为“POSIXct”类。

【问题讨论】:

    标签: r posixct


    【解决方案1】:

    1) Base R 确保格式字符串对应输入的实际格式,然后使用round。 ?strptime 给出了百分比代码的含义。

    round(as.POSIXct("01APR2019:11:51:42", format = "%d%b%Y:%H:%M:%S"), units = "hours")
    ## [1] "2019-04-01 12:00:00 EDT"
    

    2)anytime或anytime包在这种情况下无需格式字符串即可自行判断格式:

    library(anytime)
    
    round(anytime("01APR2019:11:51:42"), units = "hours")
    ## [1] "2019-04-01 12:00:00 EDT"
    

    3) lubridate 也可以使用 lubridate 包来完成:

    library(lubridate)
    
    round_date(dmy_hms("01APR2019:11:51:42", tz = ""), unit = "hour")
    ## [1] "2019-04-01 12:00:00 EDT"
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2020-02-25
      • 1970-01-01
      • 2014-12-04
      • 2020-09-20
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多