What is the most efficient way to execute raster stackApply function on very large files R?
<p>I have a large <code>rasterstack</code> <code>A</code> created from <code>&gt;11000</code> netCDF files with the following dimensions:</p>

<pre><code> library(raster) 
 library(zoo) 
 library(xts)

class       : RasterStack
dimensions  : 885, 184, 162840, 11712  (nrow, ncol, ncell, nlayers)
resolution  : 0.1, 0.1  (x, y)
</code></pre>

<p>Each layer in <code>A</code> (<code>dates=seq(as.POSIXct("2015-04-01 00:00:00"), as.POSIXct("2015-11-30 23:59:59"), by="30 mins",tz="GMT"</code>) 
represents a timestep (30mins). The task is to accumulate the time series which is at 30mins resolution to 6-hourly resolution. For a subset (144 files) of the files I can get the results within <code>20 minutes execution time</code> using:</p>

<pre><code>dates=seq(as.POSIXct("2015-04-01 00:00:00"), as.POSIXct("2015-04-03 23:59:59"), by="30 mins",tz="GMT")
index=rep(seq(1,12,by=1),each=12)
ras &lt;- setZ(A,dates)
sixh &lt;- stackApply(ras,indices=index, fun=sum);
</code></pre>

<p>Doing the same thing on <code>A</code> takes forever. Is there an efficient way to accomplish this task on <code>A</code>?</p>

<p>The same holds for all other functions within raster such as t(A), flip(A)... etc. These functions get really slow when I use them on<code>A</code>.</p>

<p>I guess it can be faster to coerce <code>A</code> to an <code>array or data.table</code> and perform calculations on that then stack back to <code>rasterstack</code>. How can this be realized on such a large file?</p>