我正在尝试将函数应用于xts对象。我正在使用ave
函数将函数分别应用于每一天。该函数抛出以下错误:
Error in rbind.zoo(...) : indexes overlap In addition: Warning messages:
1: In split.default(seq_len(nrow(xc)), f, drop = drop, ...) :
data length is not a multiple of split variable
2: In zoo(value2, i2) :
some methods for “zoo” objects do not work if the index entries in ‘order.by’ are not unique
我调试了该函数,当我尝试使用以下行将-Inf
转换为NA
时抛出错误:x[x == -Inf] <- NA
。
这是一个最小的可重复示例,仅通过ave
函数应用函数的有问题的行:
x <- as.xts(c(NA,-Inf,1,2,3,-Inf,NA,NA,NA),as.POSIXct(c(
"2010-01-05 00:00:00", "2010-01-05 00:04:00", "2010-01-05 00:08:00",
"2010-01-05 00:12:00", "2010-01-05 00:16:00", "2010-01-05 00:20:00",
"2010-01-06 00:00:00", "2010-01-06 00:04:00", "2010-01-06 00:08:00")))
out <- ave(x, as.Date(index(x)), FUN= function(x) x[x == -Inf] <- NA)
答案 0 :(得分:2)
没有理由按天分组,因为计算是逐个元素完成的:
replace(x, x == -Inf, NA)
或者如果覆盖是好的话:
x[x == -Inf] <- NA
如果您的实际函数确实使用了分组,这只是一个示例,那么将ave
应用于coredata(x)
,以便我们处理普通向量并确保应用的函数实际返回结果(在问题中没有):
fun <- function(x) replace(x, x == -Inf, NA)
x[] <- ave(coredata(x), as.Date(index(x)), FUN = fun)
我们还可以考虑使用!is.finite(x)
进行测试。
答案 1 :(得分:2)
现有答案很好,但您可以使用xts
工具轻松简洁地在时间间隔(小时,星期等)中实现平均(或任何合理的功能)(将zoo
转换为xts
)。您是否了解?period.apply
及其包装函数apply.daily
?
# Solve your overall problem in one line with no error generated when Inf values included:
x.daily <- apply.daily(x, mean, na.rm = T)
# > x.daily
# [,1]
# 2010-01-05 00:20:00 -Inf
# 2010-01-06 00:08:00 NaN
# Solve your first problem compactly (Replace +-Inf values in column 1 (generalise to any column number) of `x` with NA):
x[!is.finite(x[, 1]) &!is.na(x[, 1]), 1] <- NA
# Solve your second problem compactly (average by day):
x.daily <- apply.daily(x, FUN = mean, na.rm = T)
#Optional: tidy up timestamps in x.daily for equal spaced alignment in YYMMDD HHMMSS (NB HHMMSS will vary depending on your timezone. Should align for 86400 to 00:00:00 equivalent in UTC):
x.daily <- align.time(x.daily, 86400)
# > x.daily
# [,1]
# 2010-01-05 19:00:00 2
# 2010-01-06 19:00:00 NaN