删除日期少于完整观测值的日期 [英] Removing dates with less than Full observations
问题描述
我有一个xts对象,该对象包含169天的高频率5分钟常规观测值,但是在某些日子中缺少观测值,即少于288个数据点.如何删除这些数据,以便只有几天的时间获得完整的数据点?
I have an xts object that covers 169 days of high frequency 5 minute regular observations, but on some of the days there are missing observations, i.e less than 288 data points. How do I remove these so to have only days with full data points?
ddx = endpoints(dxts, on="days");
days = format(index(dxts)[ddx], "%Y-%m-%d");
for (day in days) {
x = dxts[day];
cat('', day, "has", length(x), "records...\n");
}
我尝试了
RTAQ::exchangeHoursOnly(dxts, daybegin = "00:00:00", dayend = "23:55:00")
但这仍然返回了完整的
谢谢
推荐答案
按天数划分.计算每天的行数,仅保留超过288行的行.
Split by days. Count the number of rows of each day, and only keep the ones that have more than 288 rows.
dxts <- .xts(rnorm(1000), 1:1000*5*60)
daylist <- lapply(split(dxts, "days"), function(x) {
if(NROW(x) >= 288) x
})
do.call(rbind, daylist)
上面的内容将dxts
划分为天".然后,如果行数大于288,则返回当天的所有数据,否则返回NULL
.因此,daylist
将是一个列表.它将具有xts
对象或NULL
的元素. do.call
部分将在列表上调用rbind
.就像调用rbind(daylist[[1]], daylist[[2]], ..., daylist[[n]])
不会聚合NULL
一样,因此您将剩下一个xts对象,该对象省略了少于288行的日期.
The above splits dxts
by "days". Then, if the number of rows is greater than 288, it returns all the data for that day, otherwise, it returns NULL
. So, daylist
will be a list. It will have elements that are either an xts
object, or NULL
. The do.call
part will call rbind
on the list. It's like calling rbind(daylist[[1]], daylist[[2]], ..., daylist[[n]])
The NULL
s won't be aggregated, so you'll be left with a single xts object that omits days with less than 288 rows.
这篇关于删除日期少于完整观测值的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!