title: Reduce/do.call to bind_rows in list of lists of data.frames
body: <p>It has been hard for me expressing this in plain English, so if anyone can edit the language, that would be greatly appreciated.</p>
  
  <p>I have a <code>list</code> object, where every element is a <code>list</code> of <code>data.frame</code> structures.</p>
  
  <p>Some of these elements in the top level list might be empty, while the others have different numbers of data.frames (always an even number though).</p>
  
  <p>My question (which seems suspiciously like the <em>opposite</em> of <a href="https://stackoverflow.com/questions/27400605/using-rapply-for-lists-of-lists-of-data-frames">this</a> one, is this:</p>
  
  <p>How can I bind the rows of the data.frames in these lists so every element of the top-level list contains two data frames? These data.frames follow the same structure every time (I want to bind the rows of the data.frame number 1,3,5,7... and the data.frames number 2,4,6,8...</p>
  
  <p>MRE as follows:</p>
  
  <pre><code>set.seed(1234)
  listy &lt;- list(`1` = list(),
                `2` = list(a = data.frame(a1 = runif(1:3), a2 = runif(1:3)),
                           b  = data.frame(a3 = runif(1:3), a4 = runif(1:3)),
                           c = data.frame(a1 = runif(1:3), a2 = runif(1:3)),
                           d = data.frame(a3 = runif(1:3), a4 = runif(1:3))))
  </code></pre>
  
  <p><code>listy</code> is a list of 2 elements (<code>1</code>,<code>2</code>). Where <code>1</code> is empty. <code>2</code> is a list of <code>data.frames</code> (each with an even number of data.frames). I want to bind the rows of <code>2</code> so every element of the top level list has 2 data.frames (if they had data.frames in the first place).</p>
  
  <p>My expected output is the following:</p>
  
  <pre><code>listb &lt;- list(`1` = list(), 
            `2` = list(structure(list(a1 = c(0.113703411305323, 0.622299404814839, 0.609274732880294, 0.282733583590016, 0.923433484276757, 0.292315840255469), a2 = c(0.623379441676661, 0.860915383556858, 0.640310605289415, 0.837295628152788, 0.286223284667358, 0.266820780001581)), .Names = c("a1", "a2"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(a3 = c(0.0094957563560456, 0.232550506014377, 0.666083758231252, 0.186722789658234, 0.232225910527632, 0.316612454829738), a3.1 = c(0.514251141343266, 0.693591291783378, 0.544974835589528, 0.302693370729685, 0.159046002896503, 0.0399959180504084)), .Names = c("a3", "a3.1"), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))))
  </code></pre>
  
  <p>Ideally, I would like to preserve <code>listy</code>, and its structure (the first element empty), the second one just with the bound rows. That is why I have tried the following, with no avail:</p>
  
  <pre><code>library(dplyr)
  lapply(length(listy), function(i) {
    #skip empty lists
    if(length(listy[[i]]) &lt; 1) {
      next
    } else {
      #make two lists
      #pairs list. even numbers
      listy[[i]][[1]] &lt;- do.call(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 0])
      #pairs list. odd numbers
      listy[[i]][[2]] &lt;- do.call(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 1])
    }
  }) 
  
  
  #another try, no positive result
  lapply(length(listy), function(i) {
    #skip empty lists
    if(length(listy[[i]]) &lt; 1) {
      next
    } else {
      #make two lists
      #pairs list. even numbers
      listy[[i]][[1]] &lt;- Reduce(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 0])
      #pairs list. odd numbers
      listy[[i]][[2]] &lt;- Reduce(bind_rows, listy[[i]][seq(1,length(listy[[i]]), by = 1) %% 2 == 1])
    }
  }) 
  </code></pre>