You tagged your question with tapply
, so here's a tapply
answer:
tapply(df[, "value"], INDEX=list(df[, "month"], df[, "date"]), FUN=mean)
# 1 2 3
# 1 -0.42965680 0.6943236 0.04505399
# 2 0.55021401 -0.3138895 -0.40966078
# 3 0.05676266 0.5212944 0.12521106
data.frame(as.table(
tapply(df[, "value"], INDEX=list(df[, "month"], df[, "date"]), FUN=mean)))
# Var1 Var2 Freq
# 1 1 1 -0.42965680
# 2 2 1 0.55021401
# 3 3 1 0.05676266
# 4 1 2 0.69432363
# 5 2 2 -0.31388954
# 6 3 2 0.52129439
# 7 1 3 0.04505399
# 8 2 3 -0.40966078
# 9 3 3 0.12521106
More common approaches, though, are aggregate
(mentioned), plyr
(mentioned), data.table
and (recently) dplyr
. The data.table
and dplyr
approaches are below.
library(data.table)
DT <- data.table(df)
DT[, mean(value), by = list(month, date)]
library(dplyr)
DF <- data.frame(df)
DF %.% group_by(month, date) %.% summarise(mean(value))
Much less common would be ave
+ unique
:
unique(within(data.frame(df), {
MV <- ave(value, month, date)
rm(value)
}))
But they all get you to the same place.