R delete rows in data frame where nrow of index is smaller than certain value

后端 未结 3 1730
眼角桃花
眼角桃花 2021-01-25 18:15

I want to delete certain rows in a data frame when the number of rows with the same index is smaller than a pre-specified value.

> fof.6.5[1:15, 1:3]
   draw          


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-25 18:31

    You could do this using dplyr (assuming your data is in a data frame called dt:

    dt %>% group_by(draw) %>% filter(n() >= 5) %>% ungroup()
    

    Or you could use table or xtabs:

    tab <- xtabs(~ draw, dt)
    
    dt[!dt$draw %in% as.numeric(names(which(tab < 5))), ]
    

提交回复
热议问题