一个data.table
解决方案
library(data.table)
A <- data.table(A)
A[, sum(v < 4), by = list(mth,day)]
## mth day V1
## 1: 1 10 2
## 2: 1 11 2
# or
A[v<4, .N, by = list(mth,day)]
## mth day N
## 1: 1 10 2
## 2: 1 11 2
基准测试
# I create a mock dataset of a `year`
library(rbenchmark)
daily <- seq(as.Date("2000/1/1"), by="day", length.out=365)
A <- data.table(mth = month(daily),day = mday(daily))
A <- A[, list(hr = 1:24), by = list(mth,day)]
A[['v']] <- sample(1:10, nrow(A), T)
# set up the various options
ddply1 <- function() ddply(A, .(mth, day), function(x) sum(x$v<4))
ddply2 <- function() ddply(A, .(mth, day), summarize, less4 = sum(v <4))
base_tapply <- function() with(A, tapply(v, paste(mth,day, sep="_"), function(x) sum(x<4) ) )
dt1 <- function() A[, sum(v < 4), by = list(mth,day)]
dt2 <- function() A[v < 4, .N, by = list(mth,day)]
sqldf_ <- function() sqldf("SELECT A.mth,A.day,sum(A.v<4) as sum FROM A GROUP BY day")
benchmark(ddply1(), ddply2(),base_tapply(),dt1(),dt2(), sqldf_(),
replications = 5,
columns = c("test", "replications", "elapsed", "relative","user.self"))
## test replications elapsed relative user.self
## 3 base_tapply() 5 0.08 8 0.08
## 1 ddply1() 5 0.72 72 0.72
## 2 ddply2() 5 1.04 104 1.03
## 4 dt1() 5 0.01 1 0.02
## 5 dt2() 5 0.00 0 0.00
## 6 sqldf_() 5 0.21 21 0.20