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

后端 未结 3 1729
眼角桃花
眼角桃花 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条回答
  •  南笙
    南笙 (楼主)
    2021-01-25 18:45

    Here is another option using data.table. Convert the 'data.frame' to 'data.table' (setDT(df1), grouped by 'draw', if the nrows (.N) are greater than or equal to 'n' then get the Subset of Data.table (.SD)

    library(data.table)
    n <- 5
    setDT(df1)[, if(.N >= n) .SD, by = draw]  
    #    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
    

    If we want only 'n' number of rows, use the head

    setDT(df1)[, if(.N >= n) head(.SD, n), by = draw]  
    

提交回复
热议问题