在R中创建一个特定的日期/时间序列 [英] Creating a specific sequence of date/times in R

查看:733
本文介绍了在R中创建一个特定的日期/时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个单列,日期/时间顺序每小时增加一年或一个月(例如)。我正在使用这样的代码来生成这个顺序:

I want to create a single column with a sequence of date/time increasing every hour for one year or one month (for example). I was using a code like this to generate this sequence:

start.date<-"2012-01-15"
start.time<-"00:00:00"
interval<-60 # 60 minutes
increment.mins<-interval*60 
x<-paste(start.date,start.time)

for(i in 1:365){
   print(strptime(x, "%Y-%m-%d %H:%M:%S")+i*increment.mins)
}

但是,我不知道如何指定日期和时间顺序的范围。此外,我在处理第一个小时00:00:00时遇到问题?不确定指定一个月,一年等日期/时间顺序的最佳方式是什么?任何建议将不胜感激。

However, I am not sure how to specify the range of the sequence of dates and hours. Also, I have been having problems dealing with the first hour "00:00:00"? Not sure what is the best way to specify the length of the date/time sequence for a month, year, etc? Any suggestion will be appreciated.

推荐答案

我强烈建议您使用 POSIXct 数据类型。这样,您可以使用 seq 而没有任何问题,并使用您想要的数据。

I would strongly recommend you to use the POSIXct datatype. This way you can use seq without any problems and use those data however you want.

start <- as.POSIXct("2012-01-15")
interval <- 60

end <- start + as.difftime(1, units="days")

seq(from=start, by=interval*60, to=end)

现在,您可以使用您的时间戳矢量来做任何事情。

Now you can do whatever you want with your vector of timestamps.

这篇关于在R中创建一个特定的日期/时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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