我有格式的时间序列数据
Ask Bid Trade Ask_Size Bid_Size Trade_Size
2016-11-01 01:00:03 NA 938.10 NA NA 203 NA
2016-11-01 01:00:04 NA 937.20 NA NA 100 NA
2016-11-01 01:00:04 938.00 NA NA 28 NA NA
2016-11-01 01:00:04 NA 938.10 NA NA 203 NA
2016-11-01 01:00:04 939.00 NA NA 11 NA NA
2016-11-01 01:00:05 NA 938.15 NA NA 19 NA
2016-11-01 01:00:06 NA 937.20 NA NA 100 NA
2016-11-01 01:00:06 938.00 NA NA 28 NA NA
2016-11-01 01:00:06 NA NA 938.10 NA NA 69
2016-11-01 01:00:06 NA NA 938.10 NA NA 831
2016-11-01 01:00:06 NA 938.10 NA NA 134 NA
时间序列数据的结构是
str(df_ts)
An ‘xts’ object on 2016-11-01 01:00:03/2016-11-02 12:59:37 containing:
Data: num [1:35797, 1:6] NA NA 938 NA 939 NA NA 938 NA NA ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "Ask" "Bid" "Trade" "Ask_Size" ...
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
NULL
我正在尝试使用以下代码每 1 分钟聚合一次数据
# Creating a Function
apply.periodly <- function (x, FUN, period, k = 1, ...)
{
if (!require("xts")) {
stop("Need 'xts'")
}
ep <- endpoints(x, on = period, k=k)
period.apply(x, ep, FUN, ...)
}
# Aggregation every minute
df_aggregate_min <- apply.periodly(x = df_ts, FUN = mean, period = "minutes", k = 1)
但是由于数据中的“NA”,我得到了错误的输出。如何通过忽略 NA 每分钟聚合列?