You can try by()
:
> with(df, by(Value, ID, mean))
# ID: 1000
# [1] 0.26
# ------------------------------------------------------------
# ID: 1001
# [1] 0.6133333
# ------------------------------------------------------------
# ID: 1002
# [1] 0.4166667
# ------------------------------------------------------------
# ID: 1003
# [1] 0.12
or aggregate()
:
> aggregate( Value ~ ID, df, mean)
# ID Value
# 1 1000 0.2600000
# 2 1001 0.6133333
# 3 1002 0.4166667
# 4 1003 0.1200000
or using data.table
(if you need fast calculation on large data sets):
> library(data.table)
> setDT(df)[, mean(Value), by = ID]
# ID V1
# 1: 1000 0.2600000
# 2: 1001 0.6133333
# 3: 1002 0.4166667
# 4: 1003 0.1200000
data
df <- structure(list(ID = c(1000L, 1000L, 1001L, 1001L, 1001L, 1002L,
1002L, 1002L, 1003L), Value = c(0.51, 0.01, 0.81, 0.41, 0.62,
0.98, 0.12, 0.15, 0.12)), .Names = c("ID", "Value"),
class = "data.frame", row.names = c(NA, -9L))