title: Sum nlayers of a rasterStack in R
body: <p>I am working with daily observation of climate data organized in .nc files.
  I read them using the stack command of the raster package. Each file (corresponding to a year) is a RasterStack element with the following characteristics:</p>
  
  <pre><code>class       : RasterStack 
  dimensions  : 360, 720, 259200, 365  (nrow, ncol, ncell, nlayers)
  resolution  : 0.5, 0.5  (x, y)
  extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax) 
  </code></pre>
  
  <p>each layer is the raster of values of a day.<br>
  I would like to sum the layers in order to calculate the monthly values. I believe the solution should be using calc or stackApply {raster}, but I couldn't find a way to sum from layer x to layer y or a way to subset the RasterStack before summing.</p>
  
  <p>I prepared an <a href="https://www.dropbox.com/s/a97crpvpa804d9x/mydata.nc?dl=0" rel="noreferrer">example file</a> with only 12 layers (to reduce the size).</p>
  
  <p>I don't exactly know how to propose a code, sorry, but it should be something like:  </p>
  
  <pre><code>library(raster)
  setwd("myfolder")
  data&lt;-stack(mydata.nc)
  
  datasum&lt;- stackApply(data, ??? ,fun=sum)
  </code></pre>
  
  <p>Thank you</p>