Keep only non-duplicate rows based on a Column Value [duplicate]

故事扮演 提交于 2019-12-02 23:51:40

问题


This is follow up to a previous question.

The dataset looks like the following:

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

I like to keep only non-duplicate rows based on first column values. The output will be like this:

    ID Veh oct nov dec jan feb
1 1120   1   7  47 152 259 140
2 1133   1   6  71 207 290 242
3 2006   1  14  72 263 331 147

A possible solution is here. But that question doesn't have reproducible example.


回答1:


We can use duplicated

dat[!(duplicated(dat$ID)|duplicated(dat$ID, fromLast = TRUE)),]


来源:https://stackoverflow.com/questions/45273036/keep-only-non-duplicate-rows-based-on-a-column-value

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