Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicated 2 columns permutations

I can't find a good title for this question so feel free to edit it please.

I have this data.frame

  section time to from
1       a    9  1    2
2       a    9  2    1
3       a   12  2    3
4       a   12  2    4
5       a   12  3    2
6       a   12  3    4
7       a   12  4    2
8       a   12  4    3

I want to remove duplicated rows that have the same to and from simultaneously, without computing permutations of the 2 columns: e.g (1,2) and (2,1) are duplicated.

So final output would be:

  section time to from
1       a    9  1    2
3       a   12  2    3
4       a   12  2    4
6       a   12  3    4

I have a solution by constructing a new column key e.g

  key <- paste(min(to,from),max(to,from))

and remove duplicated key using duplicated, but I think this is dirty solution.

here the dput of my data

structure(list(section = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = "a", class = "factor"), time = c(9L, 9L, 12L, 
12L, 12L, 12L, 12L, 12L), to = c(1L, 2L, 2L, 2L, 3L, 3L, 4L, 
4L), from = c(2L, 1L, 3L, 4L, 2L, 4L, 2L, 3L)), .Names = c("section", 
"time", "to", "from"), row.names = c(NA, -8L), class = "data.frame")
like image 540
agstudy Avatar asked Dec 29 '12 03:12

agstudy


2 Answers

mn <- pmin(s$to, s$from)
mx <- pmax(s$to, s$from)
int <- as.numeric(interaction(mn, mx))
s[match(unique(int), int),]
  section time to from
1       a    9  1    2
3       a   12  2    3
4       a   12  2    4
6       a   12  3    4

Credit for the idea goes to this question: Remove consecutive duplicates from dataframe and specifically @MatthewPlourde's answer.

like image 182
Matthew Lundberg Avatar answered Oct 04 '22 20:10

Matthew Lundberg


You can try using sort within the apply function to order the combinations.

mydf[!duplicated(t(apply(mydf[3:4], 1, sort))), ]
#   section time to from
# 1       a    9  1    2
# 3       a   12  2    3
# 4       a   12  2    4
# 6       a   12  3    4
like image 29
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 04 '22 21:10

A5C1D2H2I1M1N2O1R2T1