title: Applying a function to a specific element of a list of a list
body: <p>I have a function that returns a list of vectors and matrices. I then create a variable that is a list of several of the resulting lists from calls to the function. So I have a list of lists. My question is how do I apply a function over the elements of these lists (note this is not the same as applying a function over the lists themselves). Here is a simple example that retains all the essential features of what I am doing</p>
  
  <pre><code>numtrials = 5
  
  x = rep(list(NULL),numtrials)
  
  testfunction = function(){return( list( c(1,2,3,4,5), matrix(runif(10), 2,5),
                                      matrix(0,2,2) ) )}
  
  for(index in 1:numtrials){
      x[[index]] = testfunction()
  }
  </code></pre>
  
  <p>I want to now calculate the mean of say the (2,3) element of <code>x[[index]][[2]]</code> across all "index" lists. Or even better get a matrix of means, <code>xbar</code>, such that <code>xbar[i,j] = mean(x[[]][[2]][i,j])</code>.  I tried to play around with (and of course read the help file for) <code>lapply</code>,  and <code>apply</code>, but couldn't get it to work. One of the reasons is that <code>x[[]][[2]][i,j]</code> appears to be invalid notation </p>
  
  <pre><code>Error in x[[]] : invalid subscript type 'symbol'
  </code></pre>
  
  <p>I think R doesn't know what to make of the "<code>[[]]</code>". I know some people are going to suggest vectorizing but note that my function returns matrices and vectors of different, unrelated dimensions (although I am not opposed to vectorizing if you have a clever way of doing this). </p>