问题描述
我的脚本差不多完成了,但是日期格式有问题。 我安装了使用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"
解决方法
as_date()
使用距起点的天数来计算日期。
您正在寻找的as_datetime()
似乎也是lubridate
软件包中的软件包,该软件包使用距起点的秒数来计算日期。在您的示例中,这将是:
time <- c(3573763200,3573774000,3573784800,3573795600,3573806400,3573817200)
date <- as_datetime(time,tz = "UTC",origin = "1900-01-01") %>% date()
使用dplyr
管道和date()
中的lubridate
函数从as_datetime()
函数中提取日期。
时间是自1900年1月1日以来的秒数。使用time
中的seconds
方法,将lubridate
中的值转换为实际日期将如下工作:
lubridate::ymd("1900-01-01") + lubridate::seconds(3573763200)
您可以对其向量化:
lubridate::ymd("1900-01-01") + lubridate::seconds(time)