简体   繁体   English

将日期以来的小时数转换为r中的datetime格式

[英]convert hours since date to datetime format in r

I've downloaded some climate data from a website and am trying to analyse it in R. 我已经从网站上下载了一些气候数据,并试图在R中进行分析。

The time format for the data is hours since 1800-01-01 00:00. 数据的时间格式是从1800-01-01 00:00开始的小时数。 For example: 例如:

ss <- seq(447042,455802, length.out = 1461)

which shows data at 6 hour intervals. 每隔6小时显示一次数据。

How can I convert this to a an actual time in R. This example should give data for 1851: 如何将其转换为R中的实际时间。此示例应提供1851年的数据:

1851-01-01 00:00
1851-01-01 06:00

and so on... 等等...

How can this be done? 如何才能做到这一点?

Any advice would be appreciated. 任何意见,将不胜感激。

I think you have a typo as well as incorrect calculations. 我认为您有错别字以及错误的计算。 Let's assume you want time in the future of 1880 rather than the past. 假设您想要的是1880年的未来而不是过去的时间。 So it might be 1951 you wanted? 所以可能是您想要的1951年? Then to convert hours to seconds which are the basis for the POSIXt classed objects, just multiply by 3600 = 60*60: 然后将小时数转换为秒(这是POSIXt分类对象的基础),只需乘以3600 = 60 * 60:

> tail( as.POSIXct(ss*3600,origin='1880-01-01 00:00') )
[1] "1931-12-30 04:00:00 PST" "1931-12-30 10:00:00 PST" "1931-12-30 16:00:00 PST"
[4] "1931-12-30 22:00:00 PST" "1931-12-31 04:00:00 PST" "1931-12-31 10:00:00 PST"

As you can see it's nowhere the year 1951 either, but maybe you had two digits off and you wanted 1931? 正如您所看到的,1951年也不在话下,但是也许您有两位数的折扣并且想要1931年? Conversions that span the range of years before the onset of daylight savings time and cross century boundaries where leap years and leap seconds were used may not "line up" with your expectations. 在开始夏令时之前跨度数年的转换以及使用leap年和leap秒的跨世纪边界可能无法“符合”您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM