Remove all unique rows

后端 未结 3 2009
灰色年华
灰色年华 2020-12-11 04:33

I am trying to figure out how to remove all unique rows, from a data frame, but if it has a duplicate, I want that to stay in. For Example - I want all columns from this wi

相关标签:
3条回答
  • 2020-12-11 05:21

    Another option:

    subset(df,duplicated(col1) | duplicated(col1, fromLast=TRUE))
    
    0 讨论(0)
  • 2020-12-11 05:23

    You can do this by creating an index with ave:

    df[as.logical(ave(1:nrow(df), df$col1, FUN=function(x) length(x) > 1)), ]
    

    produces

      col1 col2 col3
    1    a    A    3
    2    a    B    3
    3    a    C    1
    6    d    A    3
    7    d    B    2
    8    d    C    1
    
    0 讨论(0)
  • 2020-12-11 05:25

    Try:

    > tdf <- table(df$col1)
    a b c d 
    3 1 1 3 
    
    df[df$col1 %in% names(tdf)[tdf>1],]
    > df
      col1 col2 col3
    1    a    A    3
    2    a    B    3
    3    a    C    1
    6    d    A    3
    7    d    B    2
    8    d    C    1
    
    0 讨论(0)
提交回复
热议问题