[R-sig-Geo] Looking for an R package for focal statistics, (moving window) within a 3D neighborhood

Gareth Davies grothered at gmail.com
Wed Sep 2 14:48:03 CEST 2015

Hi Jose,

If you end up writing your own function for this sort of thing, I'd 
advise you consider whether data.frames are the right data structure to 
use -- since they have a very high speed penalty for row-wise operations 
like what you are talking about. If all your attributes are of the same 
data type, it is much faster to use a matrix. For example, compare the 
speed of these:

x = data.frame(a = runif(1e+06), b=runif(1e+06), c=runif(1e+06))
# Simulate row-wise access and replacement
# This is slow since 'x' is a data.frame
for(i in 1:1000) x[i,] = runif(3)

# This is equivalent to above, but much faster since it uses a matrix
x = matrix(runif(3e+06), ncol=3)
for(i in 1:1000) x[i,] = runif(3)

If your data is not all of the same type, then you can't use a matrix. 
In that case you might want to experiment with using e.g. a list of 
vectors and operating on each independently, or maybe have a look at the 
data.table package [though I am less well versed with this].


More information about the R-sig-Geo mailing list