[R] Efficiently calculate sd on an array?

Gavin Simpson gavin.simpson at ucl.ac.uk
Sun Jun 17 14:47:49 CEST 2007


Dear list,

Consider the following problem:

n.obs <- 167
n.boot <- 100
arr <- array(runif(n.obs*n.obs*n.boot), dim = c(n.obs, n.obs, n.boot))
arr[sample(n.obs, 3), sample(n.obs, 3), ] <- NA

Given the array arr, with dims = 167*167*100, I would like to calculate
the sd of the values in the 3rd dimension of arr, and an obvious way to
do this is via apply():

system.time(res <- apply(arr, c(2,1), sd, na.rm = TRUE))

This takes over 4 seconds on my desktop.

I have found an efficient way to calculate the means of the 3rd
dimension using

temp <- t(rowMeans(arr, na.rm = TRUE, dims = 2))

instead of

temp <- apply(arr, c(2,1), mean, na.rm = TRUE)

but I am having difficulty seeing how to calculate the standard
deviations efficiently.

Any idea how I might go about this?

All the best,

G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT                          [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list