计算滞后分区的统计信息



我有一个data.frame,其中一列包含分类数据,一列包含日期,一列包含数值。为简单起见,请参见下面的示例:

   A        B          C
1  L    2015-12-01    5.7
2  M    2015-11-30    2.1
3  K    2015-11-01    3.2
4  L    2015-10-05    5.7
5  M    2015-12-05    1.2
6  L    2015-11-15    2.3
7  L    2015-12-03    4.4

对于A中的每个类别,我想计算滞后平均值(例如,C列中前30天值的平均值)。

我怎么也想不明白这是怎么回事。我尝试过使用sapply和一个自定义函数,该函数对类别和日期(或它的深度副本)子集data.frame,并返回统计数据(认为平均值或sd),这对于单个值来说很好,但它从sapply内部返回所有NA。

这可以做得更紧凑,但这里我把它画出来是为了让它更容易理解。核心是拆分,lapply/apply,然后将其重新组合在一起。它使用日期窗口而不是基于排序的解决方案,因此它非常通用。我还将对象放回其原始顺序,以便进行直接比较。

# set up the data
set.seed(100)
# create a data.frame with about a two-month period for each category of A
df <- data.frame(A = rep(c("K", "L", "M"), each = 60),
                 B = rep(seq(as.Date("2015-01-01"), as.Date("2015-03-01"), by="days"), 3),
                 C = round(runif(180)*6, 1))
head(df)
##   A          B   C
## 1 K 2015-01-01 1.8
## 2 K 2015-01-02 1.5
## 3 K 2015-01-03 3.3
## 4 K 2015-01-04 0.3
## 5 K 2015-01-05 2.8
## 6 K 2015-01-06 2.9
tail(df)
##     A          B   C
## 175 M 2015-02-24 4.8
## 176 M 2015-02-25 2.0
## 177 M 2015-02-26 5.7
## 178 M 2015-02-27 3.9
## 179 M 2015-02-28 2.8
## 180 M 2015-03-01 3.6
# preserve original order
df$originalOrder <- 1:nrow(df)
# randomly shuffle the order
randomizedOrder <- order(runif(nrow(df)))
df <- df[order(runif(nrow(df))), ]

# split on A - your own data might need coercion of A to a factor
df.split <- split(df, df$A)
# set the window size
window <- 30
# compute the moving average
listD <- lapply(df.split, function(tmp) {
    apply(tmp, 1, function(x) mean(tmp$C[tmp$B <= as.Date(x["B"]) & tmp$B (as.Date(x["B"]) - window)]))
})
# combine the result with the original data
result <- cbind(do.call(rbind, df.split), rollingMean = unlist(listD))
# and tidy up:
# return to original order
result <- result[order(result$originalOrder), ]
result$originalOrder <- NULL
# remove the row names
row.names(result) <- NULL  
result[c(1:5, 59:65), ]
##    A          B   C rollingMean
## 1  K 2015-01-01 1.8    1.800000
## 2  K 2015-01-02 1.5    1.650000
## 3  K 2015-01-03 3.3    2.200000
## 4  K 2015-01-04 0.3    1.725000
## 5  K 2015-01-05 2.8    1.940000
## 59 K 2015-02-28 3.6    3.080000
## 60 K 2015-03-01 1.3    3.066667
## 61 L 2015-01-01 2.8    2.800000
## 62 L 2015-01-02 3.9    3.350000
## 63 L 2015-01-03 5.8    4.166667
## 64 L 2015-01-04 4.1    4.150000
## 65 L 2015-01-05 2.7    3.860000

最新更新