George Thompson
George Thompson

Reputation: 91

How to Remove Dataframes from a List That Have Only 1 Row in R?

I have a list of dataframes, and many have only 1 row which is causing issues for ggplot. How exactly can I automate removing these dataframes?

Upvotes: 1

Views: 133

Answers (1)

akrun
akrun

Reputation: 887891

We can use Filter from base R

Filter(function(x) nrow(x) > 1, lst)

Or with sapply

lst[sapply(lst, nrow) > 1]

Or with keep from purrr

library(purrr)
keep(lst, ~ nrow(.x) > 1)

data

lst <- list(data.frame(col1 = 1, col2 = 2), data.frame(col1 = 1:5, col2 = 6:10))

Upvotes: 4

Related Questions