I think you're just looking for aggregate
. If your data.frame is named "mydf":
> aggregate(Precip ~ ., mydf, sum)
STATION DATE Precip
1 COOP 310 -7788 0.39
2 COOP 499 -7122 1.14
3 COOP 310 -6654 0.69
4 COOP 499 -2350 0.96
Judging by the size of your data, though, you might want to use data.table
instead:
> library(data.table)
data.table 1.8.8 For help type: help("data.table")
> DT <- data.table(mydf, key = "STATION,DATE")
> DT[, list(Precip = sum(Precip)), by = key(DT)]
STATION DATE Precip
1: COOP 310 -7788 0.39
2: COOP 310 -6654 0.69
3: COOP 499 -7122 1.14
4: COOP 499 -2350 0.96
Update, as per discussion in comments
Imagine your data were as follows (note the duplicated dates, but at different stations):
mydf <- structure(list(STATION = c("COOP 310", "COOP 310", "COOP 310",
"COOP 310", "COOP 499", "COOP 499", "COOP 499", "COOP 499", "COOP 499",
"COOP 499", "COOP 499"), DATE = c(-7788L, -7788L, -6654L, -6654L,
-7122L, -7122L, -7122L, -2350L, -2350L, -7788L, -7788L), Precip = c(0.24,
0.15, 0.59, 0.1, 0.64, 0.36, 0.14, 0.11, 0.85, 0.35, 0.17)), .Names = c("STATION",
"DATE", "Precip"), row.names = c(NA, 11L), class = "data.frame")
mydf
# STATION DATE Precip
# 1 COOP 310 -7788 0.24
# 2 COOP 310 -7788 0.15
# 3 COOP 310 -6654 0.59
# 4 COOP 310 -6654 0.10
# 5 COOP 499 -7122 0.64
# 6 COOP 499 -7122 0.36
# 7 COOP 499 -7122 0.14
# 8 COOP 499 -2350 0.11
# 9 COOP 499 -2350 0.85
# 10 COOP 499 -7788 0.35
# 11 COOP 499 -7788 0.17
Both alternatives presented will generate sums for the combinations of "STATION" and "DATE". Here's the data.table
process and result:
DT <- data.table(mydf, key = "STATION,DATE")
DT[, list(Precip = sum(Precip)), by = key(DT)]
# STATION DATE Precip
# 1: COOP 310 -7788 0.39
# 2: COOP 310 -6654 0.69
# 3: COOP 499 -7788 0.52
# 4: COOP 499 -7122 1.14
# 5: COOP 499 -2350 0.96