title: Aggregate raster in R with NA values
body: <p>I have a 1km resolution raster in R with widespread NA values throughout, but at irregular locations (i.e. the cells with data are not contiguous and have NA values scattered throughout). I am trying to aggregate this raster (using aggregate() command in the {raster} package) at, say, 5km resolution (factor=5) with a user-defined function for averaging circular angles (included below). As of now, I can't figure how to get aggregate() (or my function, if that's the problem) to provide a result value unless the raster has a contiguous 5x5 cell area. In other words, where the aggregate window hits a 5x5 cell area with only, say, 5 cells with data values (20 NA cells), I still want it to return an average value for those 2 cells. I've tried modifying the na.action options of both the function and the aggregate() command, with no luck. I'm not super experienced with functions, so the problem may be in there.</p>
  
  <p>Sorry for no working example, but not sure how to generate a similar example raster layer within R.</p>
  
  <p>Here's my circular average function:</p>
  
  <pre><code>library(circular)
  avg.ang &lt;- function(x,...){
    mean.circular(circular(x, units="degrees", rotation="clock", zero=pi/2, modulo="2pi"))
  }
  </code></pre>
  
  <p>And here's the aggregate code I'm using (where 'angle' is a 1km raster with NA values scattered throughout):</p>
  
  <pre><code>library(raster)
  angle5k &lt;- aggregate(angle, fact=5, fun=avg.ang, expand=T)
  </code></pre>
  
  <p>But this returns a raster layer with a value ONLY at aggregated locations where every cell of the 5x5 window contains a value.</p>