# [R] Efficiently calculate sd on an array?

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Sun, 17 Jun 2007 13:47:49 +0100

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))

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

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

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/

%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help