title: Forcing sample() in R to sample a given fraction of rows over all of k subarrays
body: <p>My problem is as follows and I have considered many derivations:</p>
  
  <p>I have an array, say with dimensions dims = c(10000, 5, 2) - that is 10 rows, 5 columns and 2 subarrays.</p>
  
  <p>I would like to be able to use the sample() function to sample a given proportion (say m) of rows in EACH subarray and move them BETWEEN subarrays.</p>
  
  <p>So, say swap Row 5 subarray 1 with Row 10 of subarray 2 (see example below).</p>
  
  <p>I asked a similar question </p>
  
  <p><a href="https://stackoverflow.com/questions/48105333/moving-rows-between-subarrays/48106091?noredirect=1#comment83475911_48106091">Moving rows between subarrays</a></p>
  
  <p>and got some great help. </p>
  
  <p>The solution is useful but restricted to random sampling of rows (meaning that not all subarrays will be sampled).</p>
  
  <pre><code>, , 1
  
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
   [1,]    4    3    4    4    3    4    5    2    4     4
   [2,]    1    4    3    5    4    5    4    5    2     4
   [3,]    1    5    2    1    1    2    1    4    5     1
   [4,]    3    1    1    3    5    4    2    4    4     4
   [5,]    3    2    5    1    2    2    5    5    4     3    &lt;-- e.g., switch this row
   [6,]    4    5    5    2    3    4    1    3    5     5
   [7,]    5    5    5    5    1    4    3    1    2     5
   [8,]    3    4    3    1    3    3    4    3    2     3
   [9,]    1    1    3    2    4    4    1    4    2     3
  [10,]    1    4    4    2    4    2    4    2    2     1
  
  , , 2
  
        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
   [1,]    5    5    1    1    5    2    1    4    3     1
   [2,]    4    3    2    4    3    5    5    5    4     3
   [3,]    2    4    1    1    4    2    2    2    3     4
   [4,]    5    1    4    5    4    4    3    4    4     5
   [5,]    1    5    5    4    3    3    5    2    2     2
   [6,]    2    2    2    2    5    5    3    4    3     5
   [7,]    5    2    1    1    2    5    3    4    4     2
   [8,]    3    4    3    3    1    3    3    2    3     5
   [9,]    2    1    4    4    3    2    4    5    5     2
  [10,]    5    3    4    5    4    3    5    1    2     3    &lt;-- with this row
  </code></pre>
  
  <p>In the above example, m = 0.10, that is 10% of the rows (1 row) in each subarray are sampled and then swapped.</p>
  
  <p>Any ideas on how to force sample() to sample within ALL subarrays? Ideally, the number of rows in each subarray will be very large (10000 or more).</p>
  
  <p>Though I have only included 2 subarrays, where a random row or rows swap(s) with a random row or rows in subarray 2 (dictated by m), I need a routine that is generalizable to k subarrays. So if k = 3, then sampling occurs within ALL subarrays and random rows are swapped with neighbouring subarrays.</p>
  
  <p>So, a random row or rows in subarray 1 has equal chance of moving to either subarray 2 or subarray 3 (it doesn't matter which subarray rows go to, so long as they are always moving between subarrays.  Then, the corresponding row or rows from subarray 2 or 3 will go to subarray 1.</p>
  
  <p>The number of rows must remain constant. For example, there cannot be 11 rows in subarray 1 and only 9 in subarray 2 -- it has to be 10 and 10.</p>
  
  <p>I don't know of any packages that will do this. The goal here is to simulate movement of animals. </p>
  
  <p>Any ideas are greatly appreciated.  </p>