R中的seq函数生成时间缺少小时? [英] missing hour in seq function generating time in R?

查看:46
本文介绍了R中的seq函数生成时间缺少小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 R 生成正确的时间序列时遇到问题:

I have a problem in generating proper time sequence using R:

fivemin <- seq(as.POSIXct("2014/01/01 0:00:00"), as.POSIXct("2014/04/01 0:00:00"), by="5 mins",tz="EST")
time <- data.frame(MasterTime=fivemin)

使用上面的代码,我可以得到一个包含 25909 个观察值的数据框.但是,在东部标准时间(没有夏令时)下,观测次数应该是 25920. 与 03/09/20014 夏令时转换相差 1 小时,因为这样时间将从凌晨 1 点变为凌晨 3 点直接AM.我不确定 R 如何处理这种时间变化.如何修改我的代码,以便 R 生成一个时间序列,而不会在 2014 年 3 月 9 日丢失凌晨 2 点?有没有人对此有任何想法?

Using above code, I can get a data frame with 25909 observations. However, under Eastern Standard Time (without daylight saving), the number of observations should be 25920. The difference is 1 hour from the transition of daylight saving time on 03/09/20014, because then the time would change from 1 AM to 3 AM directly. I'm not sure how R deals with this kind of time change. How can I revise my code so R produce a time sequence without that missing 2AM hour on 03/09/2014? Does anyone have any idea on this?

真的很感谢!

推荐答案

如果您不想使用夏令时,请尝试设置 tz = 'UTC'

If you don't want to use daylight saving time try setting tz = 'UTC'

fivemin <- seq(as.POSIXct("2014/01/01 0:00:00", tz = 'UTC'), as.POSIXct("2014/04/01 0:00:00", tz = 'UTC'), by="5 mins")
head(fivemin)
length(fivemin)
#[1] 25921

除了UTC"或GMT"之外,最好不要使用三个字母来表示时区,例如EST",因为这些含义不明确.澳大利亚有东部标准时间,美国也是.改为使用国家/城市

Apart from 'UTC' or 'GMT' it's better not to use the three letter designation for a timezone e.g "EST" because these are ambiguous. Australia has an Eastern Standard Time, as does the US. Instead use Country/City

fivemin <- seq(as.POSIXct("2014-04-06 2:00:00", tz="Australia/Melbourne"), as.POSIXct("2014-04-06 3:00:00", tz="Australia/Melbourne"), by="5 mins")
length(fivemin)
# [1] 25   
# Melbourne's DST finished on 6 April 2014 at 3 am so there are 25 x 5 min periods in the hour between 2am and 3 am 


fivemin <- seq(as.POSIXct("2014-04-06 2:00:00", tz="Australia/Brisbane"), as.POSIXct("2014-04-06 3:00:00", tz="Australia/Brisbane"), by="5 mins")
length(fivemin)
# [1] 13  Brisbane doesn't use DST so no problem

还要检查 lubridate 包中的 dst 函数.

Also check the dst function in the lubridate package.

如果您想保留时区,但使用标准时间,请尝试类似的方法.

If you would like to retain the time zone, but use standard time, try something like.

library(lubridate)
fivemin <- seq(as.POSIXct("2014-04-06 2:00:00", tz="Australia/Melbourne"), as.POSIXct("2014-04-06 3:00:00", tz="Australia/Melbourne"), by="5 mins")
fivemin[dst(fivemin)] <- fivemin[dst(fivemin)]-3600

这篇关于R中的seq函数生成时间缺少小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆