Reputation: 197
I have data
bywells <-
structure(list(Well_N = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L), .Label = c("KRT3", "KRT4"), class = "factor"), Date_m = structure(c(16251,
16281, 16312, 16343, 16373, 16312, 16343, 16373, 16404), class = "Date"),
QOM = c(132, 36, 39, 211, 45, 108, 161, 30, 31
)), class = "data.frame", row.names = c(NA, -9L), .Names = c("Well_N",
"Date_m", "QOM"))
and would like to sum variable QOM monthly and get result like
Date_m QOM
1 2014-06-30 132
2 2014-07-30 36
3 2014-08-30 147
4 2014-09-30 372
6 2014-10-30 75
7 2014-11-30 31
Whats the proper procedure to extract unique dates and get sum of QOM according to date?
EDIT: Thanks, folks. Actually both answers are perfect, but I marked as accepted the one with less points to increase karma. Thanks once more.
Upvotes: 0
Views: 81
Reputation: 326
using plyr
library(plyr)
summarize(group_by(bywells, Date_m),QOM=sum(QOM))
or base tapply
tapply(bywells$QOM, bywells$Date_m, sum)
Upvotes: 2
Reputation: 887118
Using data.table
library(data.table)
setDT(bywells)[, list(QOM=sum(QOM)), by=Date_m]
# Date_m QOM
#1: 2014-06-30 132
#2: 2014-07-30 36
#3: 2014-08-30 147
#4: 2014-09-30 372
#5: 2014-10-30 75
#6: 2014-11-30 31
Or using aggregate
from base R
aggregate(QOM~Date_m, bywells, sum)
Upvotes: 3
Reputation: 66842
There are many many ways, and dplyr
package is one option:
library(dplyr)
bywells %>% group_by(Date_m) %>% summarize(QOM = sum(QOM))
Upvotes: 3