indexing - R delete rows in data frame where nrow of index is smaller than certain value -
i want delete rows in data frame when number of rows same index smaller pre-specified value.
> fof.6.5[1:15, 1:3] draw fund.id firm.id 1 1 1667 666 2 1 1572 622 3 1 1392 553 4 1 248 80 5 1 3223 332 6 2 2959 1998 7 2 2659 1561 8 2 14233 2517 9 2 10521 12579 10 2 3742 1045 11 3 9093 10121 12 3 15681 21626 13 3 26371 70170 14 4 27633 52720 15 4 13751 656
in example, want each index have 5 rows. third draw (which index) has fewer 5 rows. how can delete draws third 1 if have fewer 5 rows?
you using dplyr
(assuming data in data frame called dt
:
dt %>% group_by(draw) %>% filter(n() >= 5) %>% ungroup()
or use table
or xtabs
:
tab <- xtabs(~ draw, dt) dt[!dt$draw %in% as.numeric(names(which(tab < 5))), ]
Comments
Post a Comment