rbind.zoo(...) : 索引重叠错误
rbind.zoo(...) : indexes overlap Error
我正在尝试将函数应用于 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)
这里没有理由按天分组,因为无论如何计算都是逐个元素进行的:
replace(x, x == -Inf, NA)
或者如果可以覆盖则:
x[x == -Inf] <- NA
如果您的实际函数确实使用了分组,而这只是一个示例,则将 ave
应用于 coredata(x)
,以便我们处理普通向量并确保函数实际应用 returns 结果(在问题中没有):
fun <- function(x) replace(x, x == -Inf, NA)
x[] <- ave(coredata(x), as.Date(index(x)), FUN = fun)
我们也可以考虑使用 !is.finite(x)
进行测试。
现有答案很好,但您也可以使用 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
我正在尝试将函数应用于 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)
这里没有理由按天分组,因为无论如何计算都是逐个元素进行的:
replace(x, x == -Inf, NA)
或者如果可以覆盖则:
x[x == -Inf] <- NA
如果您的实际函数确实使用了分组,而这只是一个示例,则将 ave
应用于 coredata(x)
,以便我们处理普通向量并确保函数实际应用 returns 结果(在问题中没有):
fun <- function(x) replace(x, x == -Inf, NA)
x[] <- ave(coredata(x), as.Date(index(x)), FUN = fun)
我们也可以考虑使用 !is.finite(x)
进行测试。
现有答案很好,但您也可以使用 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