[R-sig-Geo] raster Median function very slow on stack compared to mean: expected behavior?

Lyndon Estes lestes at princeton.edu
Fri Jul 22 04:41:21 CEST 2011


Dear List,

I am trying to extract the per-pixel median from a raster stack, and
am finding that the process takes a very long time relative to taking
the per-pixel mean. It is long enough that I have never actually seen
whether it successfully completes on my data, but this dummy example
shows the code structure I am using and the time differences between
the two functions:

stk <- stack(lapply(1:10, function(x) {
 r <- raster(nrow = 1000, ncol = 1000)
 r2 <- setValues(r, sample(1:100, size = ncell(r), replace = T, prob = NULL))
}))

setOptions(todisk = TRUE)  # I used this option to rule out memory issues
t1 <- Sys.time()
med <- calc(stk, Median, filename = "med.tif", datatype = "INT2S",
overwrite = T)
Sys.time() - t1  # 7.26 mins

t2 <- Sys.time()
med <- calc(stk, mean, filename = "mean.tif", datatype = "INT2S", overwrite = T)
Sys.time() - t2  # 2.34 secs
setOptions(todisk = FALSE)

Am I doing something wrong with my code, or is the speed difference I
am seeing expected?

Thanks in advance for your advice.

Cheers, Lyndon


> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C/en_US.UTF-8/C/C/C/C

attached base packages:
[1] splines   stats     graphics  grDevices utils     datasets
methods   base

other attached packages:
 [1] doBy_4.4.0         MASS_7.3-13        snow_0.3-5
lme4_0.999375-39   Matrix_0.999375-50 lattice_0.19-30
multcomp_1.2-6
 [8] mvtnorm_0.9-999    R2HTML_2.2         survival_2.36-9
fields_6.3         spam_0.23-0        rgdal_0.7-1        raster_1.8-39
[15] sp_0.9-83

loaded via a namespace (and not attached):
[1] grid_2.13.1   nlme_3.1-101  stats4_2.13.1 tools_2.13.1



More information about the R-sig-Geo mailing list