0

I know this question has been asked in all sorts of variants, but I could not extract the solution to my specific problem. Given a data frame like this:

a <- c(rep("A", 3), rep("B", 3), rep("C",2))
b <- c(1,1,2,4,1,1,2,2)
df <-data.frame(a,b)

This results in:

 a b
1 A 1
2 A 1
3 A 2
4 B 4
5 B 1
6 B 1
7 C 2
8 C 2

I want to only keep row number 3 (A 2) and 4 (B 4). I have tried all combinations of unique(), duplicated() and !duplicated() or distinct, but could not get the desired result, since there seems to be no combination of logical TRUE and FALSE that only filters out the non-duplicated rows. Thanks in advance!

litotes
  • 89
  • 1
  • 12
  • 1
    [This question](https://stackoverflow.com/questions/7854433/finding-all-duplicate-rows-including-elements-with-smaller-subscripts) gives you a hint, I think. – jazzurro Feb 11 '18 at 08:07
  • Thanks for the link to the answer and sorry for the duplicate. I did not come across the first solution: [link](https://stackoverflow.com/questions/13763216/how-can-i-remove-all-duplicates-so-that-none-are-left-in-a-data-frame) where the solution is: df[!(duplicated(df) | duplicated(df, fromLast = TRUE)), ] – litotes Feb 11 '18 at 08:42
  • all good. No worries. :) – jazzurro Feb 11 '18 at 09:29

0 Answers0