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

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,



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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Received on Sun 17 Jun 2007 - 12:53:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 17 Jun 2007 - 18:31:57 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.