R时间格式转换
R time format conversion
我的脚本快要完成了,但我的日期格式有问题。
我使用 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
管道和 lubridate
中的 date()
函数从 as_datetime()
函数中提取日期。
时间是自 01/01/1900 以来的秒数。将 time
中的值转换为实际日期的工作方式如下,使用 lubridate
中的 seconds
方法:
lubridate::ymd("1900-01-01") + lubridate::seconds(3573763200)
您可以将其矢量化:
lubridate::ymd("1900-01-01") + lubridate::seconds(time)
我的脚本快要完成了,但我的日期格式有问题。 我使用 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
管道和 lubridate
中的 date()
函数从 as_datetime()
函数中提取日期。
时间是自 01/01/1900 以来的秒数。将 time
中的值转换为实际日期的工作方式如下,使用 lubridate
中的 seconds
方法:
lubridate::ymd("1900-01-01") + lubridate::seconds(3573763200)
您可以将其矢量化:
lubridate::ymd("1900-01-01") + lubridate::seconds(time)