0

I have data with three columns, and I want to remove the row if column A has more than one corresponding value in column B.

My data:

   A     B      C
1  20001 66732  9526
2  20001 66748  9526
3  20001 66748  9530
4  20001 66749  9527
5  20001 66749  9528
6  20001 66749  9529
7  20001 66749  9530
8  20001 66751  9526
9  20001 66751  9527
10 20001 66751  9528

For example, the first row would not be removed, because columns A and B have only one corresponding value in column C. The second and third rows would be removed, because columns A and B have more than one corresponding value in column C. Rows 4-10 would also be removed based on the same condition.

Is there a more straightforward way of accomplishing this w/o using a loop? Any help would be appreciated.

user12310746
  • 279
  • 1
  • 10

1 Answers1

1
library(dplyr)

df %>%
        group_by(A, B) %>%
        mutate(n = n()) %>%
        filter(n == 1)
kmacierzanka
  • 747
  • 4
  • 17