Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跨R中zoo对象的行应用函数_R_Function_Zoo - Fatal编程技术网

跨R中zoo对象的行应用函数

跨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对象每一行的平均值,所以在最后我需要一个新的zoo对象,每分钟的平均值。 实际上,我正在尝试应用更高级的统计数据,但从计算角度来看,它应该与平均值相同

主管(zs)

试试这个:

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
}