Filter rows having duplicate IDs [duplicate]

淺唱寂寞╮ 提交于 2019-12-24 14:18:33

问题


My data is like this:

dat <- read.table(header=TRUE, text="
                 ID  Veh oct nov dec jan feb
1120    1   7   47  152 259 140
2000    1   5   88  236 251 145
2000    2   14  72  263 331 147
1133    1   6   71  207 290 242
2000    3   7   47  152 259 140
2002    1   5   88  236 251 145
2006    1   14  72  263 331 147
2002    2   6   71  207 290 242
")

dat
    ID Veh oct nov dec jan feb
1 1120   1   7  47 152 259 140
2 2000   1   5  88 236 251 145
3 2000   2  14  72 263 331 147
4 1133   1   6  71 207 290 242
5 2000   3   7  47 152 259 140
6 2002   1   5  88 236 251 145
7 2006   1  14  72 263 331 147
8 2002   2   6  71 207 290 242

By using duplicated function:

Unique Cells in Column 1

dat[!duplicated(dat[,1]),]

    ID Veh oct nov dec jan feb
1 1120   1   7  47 152 259 140
2 2000   1   5  88 236 251 145
4 1133   1   6  71 207 290 242
6 2002   1   5  88 236 251 145
7 2006   1  14  72 263 331 147

Duplicate cells in Column 1

dat[duplicated(dat[,1]),]
    ID Veh oct nov dec jan feb
3 2000   2  14  72 263 331 147
5 2000   3   7  47 152 259 140
8 2002   2   6  71 207 290 242

But I want to keep the row with first row like the following (which I am struggling to code):

ID    Veh  oct  nov dec jan feb
2000    1   5   88  236 251 145
2000    2   14  72  263 331 147
2000    3   7   47  152 259 140
2002    1   5   88  236 251 145
2002    2   6   71  207 290 242

回答1:


Try

dat[duplicated(dat[,1])|duplicated(dat[,1],fromLast=TRUE),]
#    ID Veh oct nov dec jan feb
#2 2000   1   5  88 236 251 145
#3 2000   2  14  72 263 331 147
#5 2000   3   7  47 152 259 140
#6 2002   1   5  88 236 251 145
#8 2002   2   6  71 207 290 242

Or

library(data.table)
setDT(dat)[, .SD[.N>1], ID]


来源:https://stackoverflow.com/questions/29740497/filter-rows-having-duplicate-ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!