Resample raster
<p>I am trying to resample a forest cover raster with high resolution (25 meters) and categorical data (1 to 13) to a new <code>RasterLayer</code> with a lower resolution (~ 1 km). My idea is to combine the forest cover data with other lower-resolution raster data :</p>

<ol>
<li><p>I tried <code>raster::resample()</code>, but since the data is categorical I lost a lot of information: </p>

<pre><code>summary(as.factor(df$loss_year_mosaic_30m))
  0       1   2   3  4   5   6   7  8   9   10  11   12  13
3777691  65  101 50 151 145 159 295 291 134 102 126 104  91 
</code></pre>

<p>As you can see, the new raster has the desired resolution but have lots of    zeros as well. I suppose that is normal since I used the ´ngb´ option in <code>resample</code>.</p></li>
<li><p>The second strategy was using <code>raster::aggregate()</code> but I find difficult to define a factor integer since the change of resolution is not straightforward (like the double of the resolution or alike). </p>

<p>My high-resolution raster has the following resolution, and I want it to aggregate it to a <code>0.008333333, 0.008333333 (x, y)</code> resolution to the same extent. </p>

<pre><code>loss_year
class       : RasterLayer 
dimensions  : 70503, 59566, 4199581698  (nrow, ncol, ncell)
resolution  : 0.00025, 0.00025  (x, y)
extent      : -81.73875, -66.84725, -4.2285, 13.39725  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : /Volumes/LaCie/Deforestacion/Hansen/loss_year_mosaic_30m.tif 
names       : loss_year_mosaic_30m 
values      : 0, 13  (min, max)
</code></pre>

<p>I have tried a factor of ~33.33 following the description of the <code>aggregate</code> help: "The number of cells is the number of cells of x divided by <code>fact*fact</code> (when fact is a single number)." Nonetheless, the resulting raster data do not seem to have the same number of rows and columns as my other low-resolution rasters. </p></li>
</ol>

<p>I have never used this high-resolution data, and I am also computationally limited (some of this commands can be parallelized using <code>clusterR</code>, but sometimes they took the same time than the non-parallelized commands, especially since they do not work for nearest neighboor calculations). </p>

<p>I am short of ideas; maybe I can try <code>layerize</code> to obtain a count raster, but I have to ´aggregate´ and the <code>factor</code> problem arises. Since this processes are taking me days to process, I do want to know the most efficient way to create a lower resolution raster without losing much information </p>

<p>A reproducible example could be the following: </p>

<pre><code>r_hr &lt;- raster(nrow=70, ncol=70) #High resolution raster with categorical data
set.seed(0)
r_hr[] &lt;- round(runif(1:ncell(r_hr), 1, 5))
r_lr &lt;- raster(nrow=6, ncol=6) #Low resolution raster
</code></pre>

<p>First strategy: loss of information</p>

<pre><code>r &lt;- resample(r_hr, r_lr, method = "ngb") #The raster data is categorical
</code></pre>

<p>Second strategy: difficult to define an aggregate factor</p>

<pre><code>r &lt;- aggregate(r_hr, factor) #How to define a factor to get exactly the same number of cells of h_lr?
</code></pre>

<p>Another option: <code>layerize</code></p>

<pre><code>r_brick &lt;- layerize(r_hr)
aggregate(r_brick, factor) #How to define factor to coincide with the r_lr dimensions? 
</code></pre>

<p>Thanks for your help! </p>