N.Varela
N.Varela

Reputation: 910

R: How to find empty dfs in a list?

I want to find empty data frames in my list. Here a basic example:

lst <- list("111.2012"=data.frame("Area"=1, "Value"=2),
            "112.2012"=data.frame("Area"=c(1:5), "Value"=c(6:10)))
lst <- lapply(lst, function(x) x[-1,])

How can I find my empty df in lst using lapply as I have more than 1500 dfs in the list? I need the element-names that are similar to the ones in the example. Thanks

Edit: Sorry, maybe I was not explicit enough in my explenation,

  1. as they were some lack of understanding, as I didn't know how to create a reproducible example with an empty df with same colnames. I create a one line df and a 5 line df and later get rid of first line --> this gives a list of one empty df and a 4 row df.

  2. I need the names of the empty dfs, I don't want to remove them as I need them.

Upvotes: 0

Views: 167

Answers (1)

akrun
akrun

Reputation: 887531

If we need to remove the empty dfs in the list

Filter(nrow, lst)

Or to get a logical vector of empty dfs

i1 <- sapply(lst, nrow)!=0
lst[i1]

Update

If we need the names of the dataframes that is empty

names(i1)[!i1]
#[1] "111.2012"

Upvotes: 3

Related Questions