R: How to keep and filter out duplicates within rows

Viewed 150

Is it possible to keep and filter out duplicates within rows?

Here is dummy data:

a <- data.frame(c('a1', 'a1', 'a1', 'a2', 'a3', 'a3'),
                  c(1, 2, 3, 1, 2, 3),
                  stringsAsFactors = FALSE)
a

colnames(a) <- c('id', 'number')
a
#   id number
# 1 a1      1
# 2 a1      2
# 3 a1      3
# 4 a2      1
# 5 a3      2
# 6 a3      3

#'Expected Result

#   id number
# 1 a1      1
# 2 a1      2
# 3 a1      3
# 5 a3      2
# 6 a3      3

As you can see, Not duplicated rows are removed from the "id" variable.

And can we adjust filtering? For example: keep and filter 3 or more duplicates within the "id" variable.

Is it achievable? dplyr approach will be helpful.

Thank you.

3 Answers

dplyr add_count + filter option as well.

a %>% 
+   add_count(id) %>% #add new column that has the frequency counts. Non duplicate will have n=1
+   filter(n>1) %>%  # remove non duplicates
+   select(-n) #remove the intemediate column

  id number
1 a1      1
2 a1      2
3 a1      3
4 a3      2
5 a3      3
subset(a, duplicated(id)|duplicated(id, fromLast = TRUE))

  id number
1 a1      1
2 a1      2
3 a1      3
5 a3      2
6 a3      3

if you are using filter:

filter(a, duplicated(id)|duplicated(id, fromLast = TRUE))

or even:

a %>%
  group_by(id) %>%
  filter(n() > 1)
by(a, a$id, \(x) if (nrow(x) > 1) x) |> 
  do.call(what = rbind) |> 
  'rownames<-'(NULL)
#>   id number
#> 1 a1      1
#> 2 a1      2
#> 3 a1      3
#> 4 a3      2
#> 5 a3      3

Created on 2022-01-06 by the reprex package (v2.0.1)

Related