-5

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.

S Das
  • 3,291
  • 6
  • 26
  • 41
  • That question doesn't have a reproducible example. This one is easy to understand. Just saying. – S Das Jul 24 '17 at 05:28
  • 4
    [Remove all rows based on multiple columns](https://stackoverflow.com/questions/36153121/remove-all-rows-based-on-multiple-columns/), [Delete rows with identical variables in R](https://stackoverflow.com/questions/38479741/delete-rows-with-identical-variables-in-r/), [remove any rows with duplicates](https://stackoverflow.com/questions/44644010/remove-any-rows-with-duplicates/) – Ronak Shah Jul 24 '17 at 05:34

1 Answers1

0

We can use duplicated

dat[!(duplicated(dat$ID)|duplicated(dat$ID, fromLast = TRUE)),]
akrun
  • 874,273
  • 37
  • 540
  • 662