簡體   English   中英

在 R 中的動物園對象的行之間應用函數

[英]Apply function across rows of zoo object in R

我想計算這個動物園對象每一行的平均值,所以最后我需要有一個新的動物園對象,每分鍾都有一個平均值。 實際上我正在嘗試應用更高級的統計數據,但從計算的角度來看,它應該與平均值相同。

頭(zs)

2014-03-09 08:00:00 1839.00 1842.00 1849.00 1838.50 1851.75 1843.50 1862.50 1874.00 1875.00 1878.00
2014-03-09 08:01:00 1838.75 1842.00 1848.75 1838.25 1851.75 1844.25 1862.50 1874.00 1875.25 1877.75
2014-03-09 08:02:00 1838.50 1842.25 1848.25 1838.25 1851.50 1843.75 1862.50 1874.00 1875.50 1878.00
2014-03-09 08:03:00 1839.25 1842.50 1848.25 1838.50 1851.50 1843.00 1862.25 1874.00 1875.50 1877.75
2014-03-09 08:04:00 1839.25 1842.50 1848.25 1838.00 1851.50 1843.00 1862.25 1874.25 1875.25 1877.75
2014-03-09 08:05:00 1838.75 1842.25 1848.25 1837.75 1851.75 1843.50 1862.25 1874.00 1875.50 1877.75

嘗試這個:

zoo(rowMeans(zs), time(zs))

或者

zoo(apply(zs, 1, mean), time(zs))

或者

Reduce(`+`, as.list(zs)) / ncol(zs)

或者

zmean <- zs[, 1]
for(i in 2:ncol(zs)) zmean <- zmean + zs[, i]
zmean <- zmean / ncol(zs)

添加了更多方法

我遇到了這個問題,想要一個更強大的解決方案。 具體來說,我想要一個功能相同的函數來apply除非在參數X傳遞了一個zoo對象(或從zoo繼承的對象,例如xts )。 在這種情況下,該函數應盡可能返回一個zoo (或xts等)對象。 結果還應保留用戶可能已附加到原始zoo對象的任何其他屬性。

有幾個挑戰。 首先,根據通過參數MARGINFUN傳遞給apply的內容, apply可能會返回一個list 、一個vector或一個維度大於 2 的array 。有關更多詳細信息,請參閱apply文檔。

因此,如果您考慮一下, apply的唯一結果可以作為zoo對象返回的情況是結果是具有nrow(result) == nrow(originalZooObject)matrix或具有length(result) == nrow(originalZooObject)的向量length(result) == nrow(originalZooObject) 否則,您無法確定如何將原始zoo對象的index值添加到apply的結果中。

我想出了以下代碼,它在結果中保留了原始zoo對象的所有適用屬性。 因此,它也適用於構建在zoo之上的大多數 S3 類,例如, xts

我所知道的唯一不能正常工作的是用戶可能附加了一個適用於原始zoo對象列的屬性。 如果FUN合並列,則用戶屬性可能不適合結果的列。

盡管我使用了從泛型函數調度的命名約定,但apply不是泛型,因此apply.zoo被設計為直接調用。 如果要使用通用版本的apply覆蓋apply函數,那么apply.zoozoo對象傳遞給applyapply.zoo就會自動運行。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM