3

I have a problem where I need to keep unique rows but not have a repeated pair e.g. if I have a table with x1=c(1,2,3) and x2=c(2,1,3) only the first and third row will be kept. The row with (2,1) is thrown out because (1,2) already exists. Can anyone help?

Thanks!

1 Answers1

3

We can sort by row using apply and get the logical index using duplicated to remove the duplicate rows.

 df1[!duplicated(t(apply(df1, 1, sort))),]
 #   x1 x2
 #1  1  2
 #2  1  3
 #3  1  1
 #4  2  4
 #6  2  2
 #7  3  4
 #8  3  2

data

df1 <- structure(list(x1 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L), x2 = c(2L, 3L, 1L, 4L, 1L, 2L, 4L, 2L, 1L, 2L, 2L)), .Names = c("x1", 
"x2"), class = "data.frame", row.names = c(NA, -11L))
akrun
  • 874,273
  • 37
  • 540
  • 662