Subset with unique cases, based on multiple columns

前端 未结 7 2174
执笔经年
执笔经年 2020-12-04 12:06

I\'d like to subset a dataframe to include only rows that have unique combinations of three columns. My situation is similar to the one presented in this question, but I\'d

7条回答
  •  孤街浪徒
    2020-12-04 12:40

    You can use the duplicated() function to find the unique combinations:

    > df[!duplicated(df[1:3]),]
      v1 v2 v3  v4 v5
    1  7  1  A 100 98
    2  7  2  A  98 97
    3  8  1  C  NA 80
    6  9  3  C  75 75
    

    To get only the duplicates, you can check it in both directions:

    > df[duplicated(df[1:3]) | duplicated(df[1:3], fromLast=TRUE),]
      v1 v2 v3 v4 v5
    3  8  1  C NA 80
    4  8  1  C 78 75
    5  8  1  C 50 62
    

提交回复
热议问题