R滞后分区的计算统计

时间:2015-10-17 00:03:21

标签: r dataframe

我有一个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),并且对单个值有效,但它返回所有NA的来自内心。

您可以给予任何帮助。

1 个答案:

答案 0 :(得分:1)

这可以更紧凑地完成,但在这里我已经把它画出来让它最容易理解。核心是分裂,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