title: R subsetting and assigning in a multidimensional array
body: <p>I am working with R with a 3D dimensional array. I am trying to use it like a set of 2D matrix for different time instants. </p>
  
  <p>I have find a behavior that I really don't understand and I will like to know <strong>why</strong> is happening. I have tried to find a explanation here and in other places but until now I still have the doubt.</p>
  
  <p>I have my 3D array like this:</p>
  
  <p><code>array3D=array(1:45,c(5,3,3))</code></p>
  
  <p>And as I expected I can access to an individual 2D matrix</p>
  
  <pre><code> array3D[1,,]
       [,1] [,2] [,3]
  [1,]    1   16   31
  [2,]    6   21   36
  [3,]   11   26   41
  </code></pre>
  
  <p>However trying to access to two 2D matrices I don't get what I expect </p>
  
  <pre><code> array3D[1:2,,]
  , , 1
  
       [,1] [,2] [,3]
  [1,]    1    6   11
  [2,]    2    7   12
  
  , , 2
  
       [,1] [,2] [,3]
  [1,]   16   21   26
  [2,]   17   22   27
  
  , , 3
  
       [,1] [,2] [,3]
  [1,]   31   36   41
  [2,]   32   37   42
  </code></pre>
  
  <p>I have find that I can solve this using <code>aperm(array3D[1:2,,])</code> but I don't understand what is doing.</p>
  
  <p>And the other problem is when I try to do an assignment, that I don't understand why this doesn't works</p>
  
  <pre><code>array3D[1:2,,]=matrix(9:1,3,3)
  array3D[1,,]
       [,1] [,2] [,3]
  [1,]    9    3    6
  [2,]    7    1    4
  [3,]    5    8    2
  </code></pre>
  
  <p>I think that I can solve this with a loop or maybe with <code>aaply</code> as I read here, but I think that if I want to work with 3D arrays is really important to understand what is happening. If someone can point me to the right direction I will be really happy.</p>
  
  <p>I have tried to find the answer here and reading <a href="http://adv-r.had.co.nz/" rel="noreferrer">http://adv-r.had.co.nz/</a> but so far no luck.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have found that everything works if instead of using the first index I use the last one, but I still doesn't understand why.
  Is something inherent to R? 
  Is possible to use the first one in some other way?</p>
  
  <pre><code>array3D=array(1:45,c(3,3,5))
  array3D[,,1:2]=matrix(9:1,3,3)
  array3D[,,2]
       [,1] [,2] [,3]
  [1,]    9    6    3
  [2,]    8    5    2
  [3,]    7    4    1
  </code></pre>