title: Loop columns of matrix with nested apply
body: <p>I am trying to loop over the columns of a matrix and change certain predefined sequences within the colomns, which are available in form of vectors.</p>
  
  <p>Let's say I have the following matrix:</p>
  
  <pre><code>m2 &lt;- matrix(sample(1:36),9,4)
  
         [,1] [,2] [,3] [,4]
  [1,]   11    6    1   14
  [2,]   22   16   27    3
  [3,]   34   10   23   32
  [4,]   21   19   31   35
  [5,]   17    9    2    4
  [6,]   28   18   29    5
  [7,]   20   30   13   36
  [8,]   26   33   24   15
  [9,]    8   12   25    7
  </code></pre>
  
  <p>As an example my vector of sequence starts is a and my vector of sequence ends is b. Thus the first sequence to delete in all columns is <code>a[1]</code> to <code>b[1]</code>, the 2nd <code>a[2]</code> to <code>b[2]</code> and so on.</p>
  
  <p>My testing code is as follows:</p>
  
  <pre><code>testing &lt;- function(x){
    apply(x,2, function(y){  
      a &lt;- c(1,5)
      b &lt;- c(2,8)
      mapply(function(y){
      y[a:b] &lt;- NA; y
      },a,b)
    })
  }
  </code></pre>
  
  <p>Expected outcome:</p>
  
  <pre><code>        [,1] [,2] [,3] [,4]
  [1,]   NA   NA   NA   NA
  [2,]   NA   NA   NA   NA
  [3,]   34   10   23   32
  [4,]   21   19   31   35
  [5,]   NA   NA   NA   NA
  [6,]   NA   NA   NA   NA
  [7,]   NA   NA   NA   NA
  [8,]   NA   NA   NA   NA
  [9,]    8   12   25    7
  </code></pre>
  
  <p>Actual result:</p>
  
  <pre><code> Error in (function (y)  : unused argument (dots[[2]][[1]])
  </code></pre>
  
  <p>What is wrong in the above code? I know I could just set the rows to NA, but I am trying to get the above output by using nested <code>apply</code> functions to learn more about them.</p>