title: How to pick the most frequent values (mode) from a raster stack
body: <p>I have four layers in a raster stack and want to pick the most frequent value in each cell among the four layers.</p>
  
  <p>Here is the dataset and code:</p>
  
  <pre><code>require(raster)
  a &lt;- raster(matrix(c(12,11,11, 
                       NA,11,11,
                       11,11,13),nrow=3))
  
  b &lt;- raster(matrix(c(12,12,12,
                       NA,12,12,
                       14,12,13),nrow=3))
  
  c &lt;- raster(matrix(c(13,9,13,
                       NA,13,13,
                       13,NA,13),nrow=3))
  
  d &lt;- raster(matrix(c(10,10,10,
                       NA,10,10,
                       10,10,10),nrow=3))
  
  stk &lt;- stack(a,b,c,d)
  </code></pre>
  
  <p>I wonder if it is possible to do it with code something like?</p>
  
  <pre><code>which.freqV &lt;- function(x, ...){
     ???
  }
  
  max &lt;- stackApply(stk,1,which.freqV,na.rm=NULL)
  </code></pre>