title: extract a column from a matrix in a list in R
body: <p>I have a list of 100 elements. Each element is a list. And this list has two elements in them. One is just a key another is a matrix. This matrix has 5 columns in it. I am trying to extract one column from each of these matrices for all the elements. So basically I would have 100 matrices again as an element of the list. This is how the list looks like</p>
  
  <pre><code>a[[1]]
       [[1]]
            [1] "keyid"
  
        [[2]] 
                [,1]                [,2]          [,3]    [,4]    [,5]
            [1,] "1329397986.572332" "x" "y" "z" "a"
            [2,] "1329242747.368294" "x" "y" "z" "a"
            [3,] "1329194224.351232 "x" "y" "z" "a"
  </code></pre>
  
  <p>So what you can see here is a is a list and it has 100 elements. Each element is a list of 2 elements. I am currently interested only in the second element. The second element has a matrix. I would like to extract the first column of this matrix. Just to see if I can extract the column into a vector I can do </p>
  
  <pre><code>a[[1]][[2]][,1] 
  </code></pre>
  
  <p>and I get all the values as a vector, as follows.</p>
  
  <pre><code>a[[1]][[2]][,1]  
         [1] "1329397986.572332" "1329242747.368294" "1329194224.351232" 
  </code></pre>
  
  <p>But when I do </p>
  
  <pre><code> x &lt;- lapply(a,function(r){r[[2]][,1]})
  </code></pre>
  
  <p>the following is the error I get.</p>
  
  <pre><code>&gt; x &lt;- lapply(a, function(r){r[[2]][,1]})
  Error in r[[2]][, 1] : incorrect number of dimensions
  </code></pre>
  
  <p>I am not sure why I am getting that. I checked the dimensions of the matrix element. </p>
  
  <pre><code>&gt; dim(a[[1]][[2]])
    [1] 52  5
  </code></pre>
  
  <p>and it looks correct. So I am not sure where the error is. </p>
  
  <p>dput(a) gives</p>
  
  <pre><code>dput(a)
  [[1]]
  [[1]][[1]]
  [1] "keyid"
  
  [[1]][[2]]
    [,1]                [,2]          [,3]    [,4]    [,5]
  [1,] "1329397986.572332" "x"   "y"     "z"     "a"
  [2,] "1329242747.368294" "x"   "y"     "z"     "a"
  [3,] "1329194224.351232" "x"   "y"     "z"     "a"
  
  [[2]]
  [[2]][[1]]
  [1] "keyid"
  
  [[2]][[2]]
    [,1]                [,2]          [,3]    [,4]    [,5]
  [1,] "1329397986.572332" "x"   "y"     "z"     "a"
  [2,] "1329242747.368294" "x"   "y"     "z"     "a"
  [3,] "1329194224.351232" "x"   "y"     "z"     "a"
  
  ...
  
  [[100]]
  [[100]][[1]]
  [1] "keyid"
  
  [[100]][[2]]
    [,1]                [,2]          [,3]    [,4]    [,5]
  [1,] "1329397986.572332" "x"   "y"     "z"     "a"
  [2,] "1329242747.368294" "x"   "y"     "z"     "a"
  [3,] "1329194224.351232" "x"   "y"     "z"     "a"
  </code></pre>