首页 > 解决方案 > R时间格式转换

问题描述

我几乎完成了我的脚本,但我的日期格式有问题。我安装了使用 as_date 函数的 lubridate 包,但它没有给我我想要的(日期)。“时间”是我的变量,我把它的描述放在下面。

我没有放我的整个脚本,因为我关心的只是这个格式问题(这意味着一个巨大的 netcdf 文件无法下载)

请问你能帮帮我吗 ?

class(time)
[1] "array"

head(time)
[1] 3573763200 3573774000 3573784800 3573795600 3573806400 3573817200

tunits
$long_name
[1] "time in seconds (UT)"
$standard_name
[1] "time"
$units
[1] "seconds since 1900-01-01T00:00:00Z"
$axis
[1] "T"
$time_origin
[1] "01-JAN-1900 00:00:00"
$conventions
[1] "relative number of seconds with no decimal part"

#conversion
date = as_date(time,tz="UTC",origin = "1900-01-01")
head(date)
[1] "-5877641-06-23" "-5877641-06-23" "-5877641-06-23" "-5877641-06-23"
[5] "-5877641-06-23" "-5877641-06-23"

标签: rdatedatetimelubridate

解决方案


自 1900 年 1 月 1 日以来的时间以秒为单位。使用以下方法将值转换为time实际日期将按以下方式工作:secondslubridate

lubridate::ymd("1900-01-01") + lubridate::seconds(3573763200)

您可以对其进行矢量化:

lubridate::ymd("1900-01-01") + lubridate::seconds(time)

推荐阅读