user2380782
user2380782

Reputation: 1446

remove data.frames in a list filled only with NA values

I have a list of data.frames and some of them are filled with NA, I would like to remove those data.frames with only NA in my list.

I am using these two commands:

list.df <- lapply(list.df, na.omit)
list.df <- list.df[sapply(list.df, function(x) dim(x)[1] >0 )]

Is there a way to do the same but in one line?

Upvotes: 1

Views: 588

Answers (1)

Beasterfield
Beasterfield

Reputation: 7113

This keeps all data.frames which have at least one NA-free row:

df.list[ sapply( df.list, function(x){ any( rowSums(is.na(x)) == 0 ) }) ]

Upvotes: 2

Related Questions