1

Here an example of a matrix,

A B C
1 1 1
1 1 4
1 2 4
2 1 1
3 1 1
3 1 2

I would like extract only rows which are unique in A and B. I can't use unique, duplicate etc. because they retain always one of my duplicated row.

In final result I wish obtain:

A B C
1 2 4
2 1 1

How can I do it? Thank you

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Seraz
  • 13
  • 2

2 Answers2

1

Here are couple of options -

  1. Base R -
cols <- c('A', 'B')
res <- df[!(duplicated(df[cols]) | duplicated(df[cols], fromLast = TRUE)), ]
res

#  A B C
#3 1 2 4
#4 2 1 1
  1. dplyr -
library(dplyr)
df %>% group_by(A, B) %>% filter(n() == 1) %>% ungroup

# A tibble: 2 x 3
#      A     B     C
#  <int> <int> <int>
#1     1     2     4
#2     2     1     1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Oh, wow, I did not understand the question like that but that makes more sense ahah. Let's see what OP thinks! – Maël Feb 28 '22 at 11:39
  • Perfect the three solution worked, even if with the dplyr I have a duplication of the A and B column. Thank you. – Seraz Mar 02 '22 at 08:36
1

data.table

df <- data.frame(
  A = c(1L, 1L, 1L, 2L, 3L, 3L),
  B = c(1L, 1L, 2L, 1L, 1L, 1L),
  C = c(1L, 4L, 4L, 1L, 1L, 2L)
)

library(data.table)
setDT(df)[, .SD[.N == 1], by = list(A, B)]
#>    A B C
#> 1: 1 2 4
#> 2: 2 1 1

Created on 2022-02-28 by the reprex package (v2.0.1)

Yuriy Saraykin
  • 8,390
  • 1
  • 7
  • 14