duplicates in multiple columns

后端 未结 2 1732
别那么骄傲
别那么骄傲 2020-11-27 15:57

I have a data frame like so

> df
  a  b c    d
1 1  2 A 1001
2 2  4 B 1002
3 3  6 B 1002
4 4  8 C 1003
5 5 10 D 1004
6 6 12 D 1004
7 7 13 E 1005
8 8 14 E          


        
相关标签:
2条回答
  • 2020-11-27 16:10

    It works if you use duplicated twice:

    df[!(duplicated(df[c("c","d")]) | duplicated(df[c("c","d")], fromLast = TRUE)), ]
    
      a  b c    d
    1 1  2 A 1001
    4 4  8 C 1003
    7 7 13 E 1005
    8 8 14 E 1006
    
    0 讨论(0)
  • 2020-11-27 16:36

    Make a new object with the 2 columns:

    df_dups <- df[c("c", "d")]
    

    Now apply it to your main df:

    df[!duplicated(df_dups),]
    

    Looks neater and easy to see/change columns that you are using.

    0 讨论(0)
提交回复
热议问题