how to remove unique entry and keep duplicates in R

后端 未结 2 1757
一生所求
一生所求 2020-12-09 11:15
ID     Cat1  Cat2    Cat3   Cat4
A0001   358 11.25   37428   0
A0001   279 14.6875 38605   0
A0013   367 5.125   40152   1
A0014   337 16.3125 38624   0
A0020   367          


        
2条回答
  •  被撕碎了的回忆
    2020-12-09 12:11

    Another option in base R Using duplicated

    dx[dx$ID %in% dx$ID[duplicated(dx$ID)],]
    
    #      ID Cat1    Cat2  Cat3 Cat4
    # 1 A0001  358 11.2500 37428    0
    # 2 A0001  279 14.6875 38605    0
    # 5 A0020  367  8.8750 37797    0
    # 6 A0020  339  9.6250 39324    0
    

    data.table using duplicated

    using duplicated and fromLast version you get :

    library(data.table)
    setkey(setDT(dx),ID) # or with data.table 1.9.5+: setDT(dx,key="ID")
    dx[duplicated(dx) |duplicated(dx,fromLast=T)]
    
    #       ID Cat1    Cat2  Cat3 Cat4
    # 1: A0001  358 11.2500 37428    0
    # 2: A0001  279 14.6875 38605    0
    # 3: A0020  367  8.8750 37797    0
    # 4: A0020  339  9.6250 39324    0
    

    This can be applied to base R also but I prefer data.table here for syntax sugar.

提交回复
热议问题