title: How to filter a nested list of dataframes by row count and remove filtered dataframes from list in R?
body: <p>This should be a simple problem to solve, but I am unable to get the exact output I would like. I have a nested list of dataframes, and I would like to filter out all dataframes with less than 50 rows, and remove them from the list.</p>
  <p>Here's a reproducible example of what I have tried -</p>
  <pre><code>L &lt;- list(iris,mtcars,iris)
  O &lt;- list(iris,mtcars,iris)
  H &lt;- list(iris,mtcars,iris)
    List &lt;- list(L,O,H)
  
  test &lt;- lapply(List, function(x) lapply(x, function(x) if (nrow(x)&lt;50) NULL else x)))
  </code></pre>
  <p>this works for the first list, but it replaces the mtcars dataframes in the nested lists with NULL - it doesn't remove them from the list. It doesn't loop through the other lists unfortunately. I have also tried using the filter function</p>
  <pre><code>test &lt;- lapply(List, function(x) lapply(x, function(x) filter(x, nrow(x)&gt;50)))
  </code></pre>
  <p>This has the same issue with not looping through all lists, and for the first list it leaves me with an empty df which is still an element of the list. My last solution was writing a for loop which I tried just on the first list in the nest, which mostly worked - but I'd like to find a less chunky way to do this if possible. This also returns an error: Error in List[[1]][[ii]] : subscript out of bounds</p>
  <pre><code>for (ii in seq_along(List[[1]])){
  n_rows = nrow(List[[1]][[ii]])
      if (n_rows &lt; 20){
          List[[1]][[ii]] = NULL
        }
      }
  </code></pre>
  <p>I am hopeful there is a simple solution just around the corner!</p>