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.
Here's a reproducible example of what I have tried -
L <- list(iris,mtcars,iris)
O <- list(iris,mtcars,iris)
H <- list(iris,mtcars,iris)
List <- list(L,O,H)
test <- lapply(List, function(x) lapply(x, function(x) if (nrow(x)<50) NULL else x)))
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
test <- lapply(List, function(x) lapply(x, function(x) filter(x, nrow(x)>50)))
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
for (ii in seq_along(List[[1]])){
n_rows = nrow(List[[1]][[ii]])
if (n_rows < 20){
List[[1]][[ii]] = NULL
}
}
I am hopeful there is a simple solution just around the corner!