Skip to content

Instantly share code, notes, and snippets.

@leeper
Last active August 29, 2015 14:20
Show Gist options
  • Save leeper/b0338c347865ff3657b4 to your computer and use it in GitHub Desktop.
Save leeper/b0338c347865ff3657b4 to your computer and use it in GitHub Desktop.
Aggregating a data.frame
z <- function(data, FUN = mean, ...) aggregate(. ~ 1, data = data, FUN = FUN, ...)
y <- function(data, FUN = mean, ...) data.frame(t(apply(data, 2, FUN = FUN, ...)))
x <- function(data, FUN = mean, ...) data.frame(t(sapply(data, mean, ...)))
d1 <- data.frame(matrix(1e3, ncol=10))
d2 <- data.frame(matrix(1e6, ncol=10))
d3 <- data.frame(matrix(1e6, ncol=100))
# MEAN
library("microbenchmark")
microbenchmark(z(d1), y(d1), x(d1), z(d2), y(d2), x(d2), z(d3), y(d3), x(d3))
Unit: microseconds
expr min lq mean median uq max neval cld
z(d1) 3816.927 4058.6360 4330.8091 4164.8650 4302.0775 7598.444 100 c
y(d1) 553.084 603.8890 666.2021 634.1020 682.0210 1966.774 100 a
x(d1) 340.626 368.9150 396.0036 380.2695 412.4080 554.624 100 a
z(d2) 3878.509 4101.3585 4461.5206 4202.9690 4415.8115 7723.917 100 c
y(d2) 556.933 603.5035 669.4929 637.3735 698.1860 2406.316 100 a
x(d2) 340.626 370.6470 414.3319 384.5035 429.1505 1914.815 100 a
z(d3) 27622.584 29516.8060 30809.0338 30358.7465 31212.8110 86128.128 100 d
y(d3) 2979.413 3193.7945 3685.8535 3352.1755 3661.0475 7326.328 100 c
x(d3) 1549.941 1715.4425 1924.5022 1775.2930 1839.7620 3719.551 100 b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment