title: Lapply within a lappy in R
body: <p>LApply-ception?
  Anyway.</p>
  <p>I have a list which is a list of dataframes. I go through it and make adjustments just fine. However I need to lapply each individual dataframe, that is, iterate through each dataframe in the list.</p>
  <p><a href="https://i.stack.imgur.com/fUxUl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fUxUl.png" alt="enter image description here" /></a></p>
  <p>So The following works.</p>
  <pre><code>#THIS WORKS
  bottleneck_fury &lt;- function(bottleneck2) {
    
    bottleneck2 &lt;- bottleneck2 %&gt;% 
      mutate(startTime = as_datetime(startTime, tz = &quot;&quot;)) %&gt;% 
      mutate(endTime = as_datetime(endTime, tz = &quot;&quot;)) %&gt;% 
      mutate(early_startTime = startTime - 300) %&gt;% #5 min prior
      mutate_at(c(&quot;startTime&quot;, &quot;endTime&quot;, &quot;early_startTime&quot;),anytime) %&gt;%  #formatted time
      mutate(id = rownames(bottleneck2)) 
    
    bottleneck2 
  }
  
  all_necks &lt;- lapply(bottleneck_test, bottleneck_fury)
  dd &lt;- all_necks[[1]]
  
  
  OUTPUT
  dd &lt;- structure(list(startTime = structure(c(1533122400, 1533132060, 
  1533151920, 1533205740, 1533207960), class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;
  ), tzone = &quot;&quot;), endTime = structure(c(1533131340, 1533132540, 
  1533153300, 1533207660, 1533218460), class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;
  ), tzone = &quot;&quot;), impact = c(627.06, 26.53, 24.34, 166.84, 761.39
  ), impactPercent = c(1444.6, 33.98, 30.98, 320.75, 1632.12), 
      impactSpeedDiff = c(25814.55, 806.43, 733.5, 6289.26, 30350.4
      ), maxQueueLength = c(4.829494, 3.605648, 2.241074, 5.760513, 
      5.760513), tmcs = list(c(&quot;110N04623&quot;, &quot;110-04623&quot;, &quot;110N04624&quot;, 
      &quot;110-04624&quot;, &quot;110N04625&quot;, &quot;110-04625&quot;, &quot;110N04626&quot;, &quot;110-04626&quot;
      ), c(&quot;110N04623&quot;, &quot;110-04623&quot;, &quot;110N04624&quot;, &quot;110-04624&quot;, 
      &quot;110N04625&quot;, &quot;110-04625&quot;), c(&quot;110N04623&quot;, &quot;110-04623&quot;, &quot;110N04624&quot;, 
      &quot;110-04624&quot;), c(&quot;110N04623&quot;, &quot;110-04623&quot;, &quot;110N04624&quot;, &quot;110-04624&quot;, 
      &quot;110N04625&quot;, &quot;110-04625&quot;, &quot;110N04626&quot;, &quot;110-04626&quot;, &quot;110N04627&quot;
      ), c(&quot;110N04623&quot;, &quot;110-04623&quot;, &quot;110N04624&quot;, &quot;110-04624&quot;, 
      &quot;110N04625&quot;, &quot;110-04625&quot;, &quot;110N04626&quot;, &quot;110-04626&quot;, &quot;110N04627&quot;
      )), early_startTime = structure(c(1533122100, 1533131760, 
      1533151620, 1533205440, 1533207660), class = c(&quot;POSIXct&quot;, 
      &quot;POSIXt&quot;), tzone = &quot;&quot;), id = c(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;)), row.names = c(NA, 
  5L), class = &quot;data.frame&quot;)
  
  </code></pre>
  <p>Now that I pull this dataframe out and manipulate it, I want to run a loop on it. However the result is a list when I need it to be a dataframe. The second lapply here is now changed to represent the actual case and why unlist() wont work.</p>
  <pre><code>#DOESNT WORK
  bottleneck_fury &lt;- function(bottleneck2) {
    
   # bottleneck2 &lt;- data.frame(matrix(unlist(bottleneck), nrow=length(bottleneck), byrow=T))
    
    bottleneck2 &lt;- bottleneck2 %&gt;% 
      mutate(startTime = as_datetime(startTime, tz = &quot;&quot;)) %&gt;% 
      mutate(endTime = as_datetime(endTime, tz = &quot;&quot;)) %&gt;% 
      mutate(early_startTime = startTime - 300) %&gt;% #5 min prior
      mutate_at(c(&quot;startTime&quot;, &quot;endTime&quot;, &quot;early_startTime&quot;),anytime) %&gt;%  #formatted time
      mutate(id = rownames(bottleneck2)) 
    
    bottleneck2 
    #md_events2 &lt;-  md_events 
    
    
    test &lt;- lapply(1:nrow(bottleneck2), function(x) {
      #go row by row
      bottleneck_row &lt;- bottleneck2[x,]
      events &lt;- iris[which(bottleneck_row$maxQueueLength &lt; iris$Petal.Length ==TRUE),]
  
      ID &lt;- do.call(&quot;rbind&quot;,replicate(nrow(events), bottleneck_row, simplify = FALSE))
      cbind(events, ID)
  }
  
  }
  
  all_necks &lt;- lapply(bottleneck_test, bottleneck_fury)
  dd &lt;- all_necks[[1]]
  View(dd)
  </code></pre>
  <p>My wish here is that R can do the lapply within the lapply and give me a new dataframe for both of my dataframes in the list.</p>
  <p>In the end, dd should be a dataframe which is the first dataframe in the list <code>all_necks</code>.</p>