title: monthly means with apply for multidimensional arrays
body: <p>I want to compute the mean over the 3-D of a multidimensional array. As this dimension is supposed to be the time, I wanted to computed monthly means. For that, I tried to use apply, but I am not sure where the problem is. Let's say my data is as the following:</p>
  
  <pre><code>       #Creating a sample  
         m &lt;-array(1:12, dim=c(20,4,36))
         #number of months
         months &lt;- seq(1:12)
         #Compute the mean over each month (dimension of the result should be [20,4,12]
         monmean &lt;- apply(m,1:2,function(x) for(i in 1:12) mean(x[,,months==i],na.rm=TRUE))
  </code></pre>
  
  <p>Any idea?? 
  Thanks in advance</p>