Zorro
Zorro

Reputation: 101

R List delete all list entries which have one row

I have data which I list with the function, lapply:

For example:

library(data.table)

# example data
data <- data.frame(D = rep(c("111"), 8),
                    I = c(rep("2012", 5), "2014", "2013", "2013"),
                    S = rep(c("1000", "2000"), 4))
list=lapply(X=colnames(data),FUN=function(X) setDT(data)[, .(Anzahl = .N), by = X][order(-Anzahl),])

Now I want to delete all list entries which have just 1 row: Here just the first one but it can be much more.

Alternatively I can remove all the columns which have just one factor (In Example D, because they are all the same).

Upvotes: 2

Views: 237

Answers (1)

Parfait
Parfait

Reputation: 107577

Simply use Filter() to keep data table elements with rows greater than one:

list <- Filter(function(dt) nrow(dt) > 1, list)

Upvotes: 1

Related Questions