Home > Net >  Show rows that appear only once in R dataframe
Show rows that appear only once in R dataframe

Time:05-17

I know that we can use unique() to effectively show a dataframe without duplicate values, but is there an elegant way to show only those rows that appear once in a dataframe?

E.g.,

a = c(10,20,10,10)
b = c(10,30,10,20)
ab = data.frame(a,b)

should return the second and final row only, and not the first and third (since this row exists more than once).

Thanks

CodePudding user response:

We can use duplicated

subset(ab, !(duplicated(ab)|duplicated(ab, fromLast = TRUE)))

-output

   a  b
2 20 30
4 10 20

CodePudding user response:

dplyr option:

library(dplyr)
ab %>%
  group_by(across(everything())) %>%
  filter(n() == 1)

Output:

# A tibble: 2 × 2
# Groups:   a, b [2]
      a     b
  <dbl> <dbl>
1    20    30
2    10    20
  • Related