[英]Generate a uniformly sampled time series object in R
Hi I am looking to generate a uniformly sampled time series at 30 minute interval from a particular start date to some end date. 嗨,我正在寻找一个从特定开始日期到某个结束日期的30分钟间隔的统一采样时间序列。 However the constraint is that on each day the 30 minute interval begins at 7:00 and ends at 18:30 ie I need the time series object to be something like 但是,约束条件是每天30分钟的时间间隔从7:00开始,到18:30结束,即我需要时间序列对象类似于
c('2016-08-19 07:00:00',
'2016-08-19 07:30:00',
...,
'2016-08-19 18:30:00',
'2016-08-20 07:00:00',
...,
'2016-08-20 18:30:00',
...
'2016-08-31 18:30:00')
Without the constraints it can be done with something like 没有约束,可以用类似的方法完成
seq(as.POSIXct('2016-08-19 07:00:00'), as.POSIXct('2016-08-21 18:30:00'), by="30 min")
But I dont want the times between '2016-08-20 18:30:00' and '2016-08-21 07:30:00' in this case. 但在这种情况下,我不希望介于'2016-08-20 18:30:00'和'2016-08-21 07:30:00'之间的时间。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!
Using the example series you created: 使用您创建的示例系列:
ts <- seq(as.POSIXct('2016-08-19 07:00:00'),
as.POSIXct('2016-08-21 18:30:00'), by="30 min")
Pull out the hours from your series using strftime
: 使用strftime
从系列中抽出时间:
hours <- strftime(ts, format="%H:%M:%S")
> head(hours)
[1] "07:00:00" "07:30:00"
[3] "08:00:00" "08:30:00"
[5] "09:00:00" "09:30:00"
You can then convert it back to POSIXct
: 然后,您可以将其转换回POSIXct
:
hours <- as.POSIXct(hours, format="%H:%M:%S")
This will retain the times of the day but it will make the date today's date: 这将保留一天中的时间,但将日期设置为今天的日期:
> head(hours)
[1] "2016-09-11 07:00:00 EDT"
[2] "2016-09-11 07:30:00 EDT"
[3] "2016-09-11 08:00:00 EDT"
[4] "2016-09-11 08:30:00 EDT"
[5] "2016-09-11 09:00:00 EDT"
[6] "2016-09-11 09:30:00 EDT"
> tail(hours)
[1] "2016-09-11 16:00:00 EDT"
[2] "2016-09-11 16:30:00 EDT"
[3] "2016-09-11 17:00:00 EDT"
[4] "2016-09-11 17:30:00 EDT"
[5] "2016-09-11 18:00:00 EDT"
[6] "2016-09-11 18:30:00 EDT"
You can then create a TRUE/FALSE vector based on the condition you want: 然后,您可以根据所需条件创建TRUE / FALSE向量:
condition <- hours > "2016-09-11 07:30:00 EDT" &
hours < "2016-09-11 18:30:00 EDT"
Then filter your original series based on this condition: 然后根据以下条件过滤您的原始系列:
ts[condition]
Here is my short and handy solution with package lubridate
这是我使用lubridate
包裹的简便解决方案
library("lubridate")
list <- lapply(0:2, function(x){
temp <- ymd_hms('2016-08-19 07:00:00') + days(x)
result <- temp + minutes(seq(0, 690, 30))
return(strftime(result))
})
do.call("c", list)
I have to use strftime(result)
to remove the timezone and to have the right times. 我必须使用strftime(result)
删除时区并拥有正确的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.