2

I have a list of dataframes, ranging in size. I want to drop those with few rows (lets say <3).

This is what I've been working with so far, for what it's worth:

d1 <- data.frame(y1 = c(1, 2, 3, 9), y2 = c(4, 5, 6, 7))
d2 <- data.frame(y1 = c(3, 2, 1, 6), y2 = c(6, 5, 4, 4))
d3 <- data.frame(y1 = c(3, 2), y2 = c(6, 5))
d4 <- data.frame(y1 = c(3, 2), y2 = c(6, 5))
listdfs <- list(d1, d2, d3, d4)


listdfs2<-lapply(listdfs,function(x) if(nrow<10<-NULL)))

So basically d1 and 2 should be maintained.

Thanks.

FatBerg
  • 137
  • 6
  • A possible duplicate of https://stackoverflow.com/questions/6941506/subset-elements-in-a-list-based-on-a-logical-condition/8935399. – Majid Oct 17 '19 at 13:03

3 Answers3

3

This can be done by Filter, i.e.

Filter(function(i) nrow(i) > 3, listdfs)

[[1]]
  y1 y2
1  1  4
2  2  5
3  3  6
4  9  7

[[2]]
  y1 y2
1  3  6
2  2  5
3  1  4
4  6  4
Sotos
  • 51,121
  • 6
  • 32
  • 66
2

You can do:

listdfs[lapply(listdfs, nrow) > 3]

[[1]]
  y1 y2
1  1  4
2  2  5
3  3  6
4  9  7

[[2]]
  y1 y2
1  3  6
2  2  5
3  1  4
4  6  4

Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34
2

Another option is to use discard from purrr

purrr::discard(listdfs, ~ nrow(.) < 3)

[[1]]
  y1 y2
1  1  4
2  2  5
3  3  6
4  9  7

[[2]]
  y1 y2
1  3  6
2  2  5
3  1  4
4  6  4

Matt
  • 2,947
  • 1
  • 9
  • 21