2

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).

Parfait
  • 104,375
  • 17
  • 94
  • 125
Zorro
  • 91
  • 1
  • 8
  • 1
    Always ask your question on stackoverflow as a reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Without knowing your data, if you want to run functions over lists, you should use `lapply` or `sapply` again. For example, `sapply(list, nrow)` – Evan Friedland Sep 22 '18 at 13:53
  • Ok Sry, I edit a example. – Zorro Sep 22 '18 at 14:42
  • Kindly share you data using `head` or `dput`. – Saurabh Chauhan Sep 22 '18 at 15:13
  • @SaurabhChauhan ... load data.table and OP's code should reproduce list. – Parfait Sep 22 '18 at 15:14

1 Answers1

1

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

list <- Filter(function(dt) nrow(dt) > 1, list)
Parfait
  • 104,375
  • 17
  • 94
  • 125