title: R: change order of layers in rasterstack
body: <p>I have two sets of rasterstacks (each with a few hundred raster layers) - the first is a rasterstack containing a set of sensored time series images (with gap) and a second stack of temporally interpolated images for the gaps of the first set. Naming of the layers in each set is according to the day they where measured/or interpolated, starting from day 1....n</p>
  
  <p>I now want to combine these two sets into one ordered (from 1 to n according to layer name) rasterstack. I have looked into different ways of doing this but haven't been able to get the results;</p>
  
  <ul>
  <li><p>A way to order the layers within a rasterstack (for example by using something like this (comb_r is the raster stack from my reproducible example below). This reorders the names but not the entire layers:</p>
  
  <pre><code>names(comb_r)&lt;-comb_r[order(names(comb_r))]
  </code></pre></li>
  <li><p>create two lists of layers in both stacks using the unstack function and than create a combined ordered list as an input to a new stack operation (did not get this to work).</p></li>
  <li><p>finally I guess I could save all layers onto the hard disk and than reassemble a stack from there (considering the many layers probably not the best way forward).</p></li>
  </ul>
  
  <p>Any suggestions on how to proceed would be welcome. I have added a toy example of my problem here:</p>
  
  <pre><code>library(raster)
  
  r1 &lt;- raster(matrix(runif(9), ncol = 3))
  r2 &lt;- raster(matrix(runif(9), ncol = 3))
  r3 &lt;- raster(matrix(runif(9), ncol = 3))
  r4 &lt;- raster(matrix(runif(9), ncol = 3))
  r5 &lt;- raster(matrix(runif(9), ncol = 3))
  
  r &lt;- stack(r1, r2, r3,r4,r5)
  names(r)&lt;-c(1,4,6,8,10)
  
  r6 &lt;- raster(matrix(runif(9), ncol = 3))
  r7 &lt;- raster(matrix(runif(9), ncol = 3))
  r8 &lt;- raster(matrix(runif(9), ncol = 3))
  r9 &lt;- raster(matrix(runif(9), ncol = 3))
  r10 &lt;- raster(matrix(runif(9), ncol = 3))
  
      rr &lt;- stack(r6,r7,r8,r9,r10)
  
  names(rr)&lt;-c(2,3,5,7,9)
  
  comb_r&lt;-stack(r,rr)
  </code></pre>