跨R中zoo对象的行应用函数
我想计算这个zoo对象每一行的平均值,所以在最后我需要一个新的zoo对象,每分钟的平均值。 实际上,我正在尝试应用更高级的统计数据,但从计算角度来看,它应该与平均值相同 主管(zs) 试试这个:跨R中zoo对象的行应用函数,r,function,zoo,R,Function,Zoo,我想计算这个zoo对象每一行的平均值,所以在最后我需要一个新的zoo对象,每分钟的平均值。 实际上,我正在尝试应用更高级的统计数据,但从计算角度来看,它应该与平均值相同 主管(zs) 试试这个: zoo(rowMeans(zs), time(zs)) 或 或 或 zmean我遇到了这个问题,想要一个更健壮的解决方案。具体来说,我想要一个与apply工作相同的函数,除非zoo对象(或继承自zoo的对象,例如xts)在参数X中传递。在这种情况下,函数应尽可能返回zoo(或xts等)对象。结果还应保
zoo(rowMeans(zs), time(zs))
或
或
或
zmean我遇到了这个问题,想要一个更健壮的解决方案。具体来说,我想要一个与apply
工作相同的函数,除非zoo
对象(或继承自zoo
的对象,例如xts
)在参数X
中传递。在这种情况下,函数应尽可能返回zoo
(或xts
等)对象。结果还应保留用户可能附加到原始zoo
对象的任何附加属性
有几个挑战。首先,根据通过参数MARGIN
和FUN
传递给apply
的内容,apply
可能返回维度大于2的列表、向量或数组。有关更多详细信息,请参阅apply
文档
因此,如果您仔细考虑一下,apply
的唯一时间结果可以作为zoo
对象返回,如果结果是一个nrow(result)==nrow(originalZooObject)
的矩阵
,或者是一个length(result)==nrow(originalZooObject)
的向量。否则,您无法确定如何将原始zoo
对象的索引值添加到apply
的结果中
apply.zoo = function(X, MARGIN, FUN, ...) {
result = apply(as.matrix(X), MARGIN, FUN, ...)
# if result is not a list and X is a zoo object, we can try to convert result
# to zoo.
if(!is.list(result) | is.zoo(X)) {
# if FUN was applied across rows and has the right length but simplfied to a
# vector, convert result back to a matrix
if(is.null(dim(result)) & MARGIN[1] == 1 & length(result) == nrow(X)) {
result = matrix(result, ncol = 1, dimnames = list(NULL, "result"))
}
# if we don't have a matrix at this point, we can't convert back to a zoo object.
if(is.matrix(result)) {
# the matrix returned by apply sometimes has dates as columns and series as
# rows. Check for this and transpose.
if(identical(dimnames(result)[[1]], dimnames(X)[[2]])) result = t(result)
# if result has maintained the same number of rows, it can be restored to a
# zoo object.
if(identical(nrow(X), nrow(result))){
# first ensure the dimname of rows is NULL
dimnames(result)[1] = list(NULL)
# then restore all attributes, other than dim and dimnames
result =
do.call(
structure,
c(
list(result),
attributes(X)[-which(names(attributes(X)) %in% c("dim", "dimnames"))]
)
)
}
}
}
result
}
我提出了以下代码,它在结果中保留了原始zoo
对象的所有适用属性。因此,它还可以用于构建在zoo
之上的大多数S3类,例如xts
我所知道的唯一一件事是,用户可能附加了一个应用于原始zoo
对象的列的属性。如果FUN
合并列,则用户属性可能不适合结果的列
虽然我使用命名约定从泛型函数进行分派,apply
不是泛型函数,因此apply.zoo
被设计为直接调用。如果要用通用版本的apply
覆盖apply
函数,则apply.zoo
将在zoo
对象传递给apply
时自动运行
apply.zoo = function(X, MARGIN, FUN, ...) {
result = apply(as.matrix(X), MARGIN, FUN, ...)
# if result is not a list and X is a zoo object, we can try to convert result
# to zoo.
if(!is.list(result) | is.zoo(X)) {
# if FUN was applied across rows and has the right length but simplfied to a
# vector, convert result back to a matrix
if(is.null(dim(result)) & MARGIN[1] == 1 & length(result) == nrow(X)) {
result = matrix(result, ncol = 1, dimnames = list(NULL, "result"))
}
# if we don't have a matrix at this point, we can't convert back to a zoo object.
if(is.matrix(result)) {
# the matrix returned by apply sometimes has dates as columns and series as
# rows. Check for this and transpose.
if(identical(dimnames(result)[[1]], dimnames(X)[[2]])) result = t(result)
# if result has maintained the same number of rows, it can be restored to a
# zoo object.
if(identical(nrow(X), nrow(result))){
# first ensure the dimname of rows is NULL
dimnames(result)[1] = list(NULL)
# then restore all attributes, other than dim and dimnames
result =
do.call(
structure,
c(
list(result),
attributes(X)[-which(names(attributes(X)) %in% c("dim", "dimnames"))]
)
)
}
}
}
result
}
非常感谢,将zoo zs对象的索引从index(zs)更改为dot-use POSIXlt之后,它工作得很好。POSIXct确实有效。如果看起来不是这样,那么你就犯了其他一些错误。
Reduce(`+`, as.list(zs)) / ncol(zs)
zmean <- zs[, 1]
for(i in 2:ncol(zs)) zmean <- zmean + zs[, i]
zmean <- zmean / ncol(zs)
apply.zoo = function(X, MARGIN, FUN, ...) {
result = apply(as.matrix(X), MARGIN, FUN, ...)
# if result is not a list and X is a zoo object, we can try to convert result
# to zoo.
if(!is.list(result) | is.zoo(X)) {
# if FUN was applied across rows and has the right length but simplfied to a
# vector, convert result back to a matrix
if(is.null(dim(result)) & MARGIN[1] == 1 & length(result) == nrow(X)) {
result = matrix(result, ncol = 1, dimnames = list(NULL, "result"))
}
# if we don't have a matrix at this point, we can't convert back to a zoo object.
if(is.matrix(result)) {
# the matrix returned by apply sometimes has dates as columns and series as
# rows. Check for this and transpose.
if(identical(dimnames(result)[[1]], dimnames(X)[[2]])) result = t(result)
# if result has maintained the same number of rows, it can be restored to a
# zoo object.
if(identical(nrow(X), nrow(result))){
# first ensure the dimname of rows is NULL
dimnames(result)[1] = list(NULL)
# then restore all attributes, other than dim and dimnames
result =
do.call(
structure,
c(
list(result),
attributes(X)[-which(names(attributes(X)) %in% c("dim", "dimnames"))]
)
)
}
}
}
result
}