D <- matrix(rnorm(2000), nrow=2, ncol=1000)
t(matrix(c(quantile(D[1,], c(0.05,0.95)), quantile(D[2,], c(0.05,0.95))), nrow=2))
I have a 2-by-1000 matrix, each of whose columns is a pair of observations of (X,Y). I want to find the same quantiles of each row. say q_0.05 and q_0.95. What is the fastest way to compute that?
Try matrixStats::rowQuantiles
.
library(matrixStats)
microbenchmark::microbenchmark(baseR=apply(D, 1, quantile, c(0.05, 0.95)),
matrixStats=rowQuantiles(D, probs=c(.05, .95)),
times=10L)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# baseR 222.127 227.1580 238.7553 229.6283 233.1329 326.8730 10 b
# matrixStats 145.262 160.9838 171.9204 161.8530 168.4477 263.1476 10 a
y1 <- t(apply(D, 1, quantile, c(.05, .95)))
y2 <- rowQuantiles(D, probs=c(.05, .95))
stopifnot(all.equal(y1, y2))
Data:
set.seed(42)
D <- matrix(rnorm(2e6), nrow=2, ncol=2e6)