1

I use duplicate results to estimate the measurement uncertainty for chemical analyses. When I extract data from the laboratory database it consists largely of single results but with some samples tested twice, some more than twice (I have seen up to 12). I want to discard all the single analyses and just retain the duplicated results, but including the original result.

The samples are identified by a sample number that is common to the duplicate samples.

I can pull out the duplicates using duplicated() but how to I retain the first result as well?

Thanks.

Lee_Kennedy
  • 207
  • 1
  • 2
  • 12

1 Answers1

3
> dat <- data.frame(
    id = sample(1:5, 10, replace = TRUE),
    x = rnorm(10)
    )

> dat
##    id          x
## 1   1  0.7060512
## 2   4  0.6804117
## 3   2  0.2395902
## 4   2  1.5352574
## 5   1  0.2376593
## 6   4  0.8019506
## 7   1 -1.0506505
## 8   5  1.0554555
## 9   3  0.3637685
## 10  5 -0.8404215
> dat[duplicated(dat$id) | duplicated(dat$id, fromLast = TRUE),]
##    id          x
## 1   1  0.7060512
## 2   4  0.6804117
## 3   2  0.2395902
## 4   2  1.5352574
## 5   1  0.2376593
## 6   4  0.8019506
## 7   1 -1.0506505
## 8   5  1.0554555
## 10  5 -0.8404215
Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32