title: Mosaic rasterstacks using minimum of certain layer
body: <p>I am trying to mosaic 42 remote sensing rasterstacks (with 250 bands) based on the criterion that in overlapping areas, the pixel should be taken that has the most nadir viewing angle </p>
  
  <p>Beside my rasterstacks I also have 42 rasters (so one for each stack) with the corresponding viewing angle for each pixel.</p>
  
  <p>Any idea how to solve this?</p>
  
  <p>I tried to include the viewing angle raster in the stack, and use something similar to </p>
  
  <pre><code>mosaic(a,b,fun=function(x)(min(x[[251]])) 
  </code></pre>
  
  <p>but that didn't work...</p>
  
  <p>Any advice?</p>
  
  <p>Thanks in advance,</p>
  
  <p>R.</p>