当有重复时,丢弃一行

时间:2014-04-15 17:09:57

标签: r

假设我有一个数据框;

signers <- data.frame(
        id = c(11,11,12,13,14,15,16,17,17,18,19,20,20,21) ,
        first = 
            c("Benjamin","Ben","Robert","George","Thomas","Jared","James","John","James","George","George","James","Edmund","George") ,
        last = 
            c( "Franklin","F","Morris","Clymer","Fitzsimons","Ingersoll","Wilson","Blair","Madison","Washington","Mason","McClurg","E","Wythe")
        )

看起来像这样......

id    first       last
1  11 Benjamin   Franklin
2  11      Ben          F
3  12   Robert     Morris
4  13   George     Clymer
5  14   Thomas Fitzsimons
6  15    Jared  Ingersoll
7  16    James     Wilson
8  17     John      Blair
9  17    James    Madison
10 18   George Washington
11 19   George      Mason
12 20    James    McClurg
13 20   Edmund          E
14 21   George      Wythe

我想删除每个重复ID号的第二,第三,第四等出现。所以在这种情况下,我想要一个没有第2,9和13行的数据框。

1 个答案:

答案 0 :(得分:2)

只需使用duplicated

signers[!duplicated(signers$id), ]
#    id    first       last
# 1  11 Benjamin   Franklin
# 3  12   Robert     Morris
# 4  13   George     Clymer
# 5  14   Thomas Fitzsimons
# 6  15    Jared  Ingersoll
# 7  16    James     Wilson
# 8  17     John      Blair
# 10 18   George Washington
# 11 19   George      Mason
# 12 20    James    McClurg
# 14 21   George      Wythe