0

I am using R and I would like to remove combinations in a data.frame. Unique function does not seem to do the job.

  a b c
1 1 4 A
2 2 3 B
3 1 5 C
4 4 1 A
5 5 1 C
6 3 2 B
7 3 2 E

And I would like to get something that looks like this, keeping just one combination of column a and b (column c is a frequency measure):

       a b c
     1 1 4 A
     2 2 3 B
     3 1 5 C

Thanks a lot!

PS: The origin of the problem is the dcast function returning this error: Aggregation function missing: defaulting to length (R reshape2 'Aggregation function missing: defaulting to length')

Otto_Shmotto
  • 23
  • 1
  • 5
  • 2
    Possible duplicate of [Remove duplicates column combinations from a dataframe in R](https://stackoverflow.com/questions/8422415/remove-duplicates-column-combinations-from-a-dataframe-in-r) and [removing-duplicate-combinations-irrespective-of-order](https://stackoverflow.com/questions/9028369/removing-duplicate-combinations-irrespective-of-order) – phiver Sep 02 '18 at 10:08
  • Thank you very much. You have been very helpful! – Otto_Shmotto Sep 02 '18 at 13:03

1 Answers1

5
df[!duplicated(t(apply(df[c("a", "b")], 1, sort))), ]
  a b c
1 1 4 A
2 2 3 B
3 1 5 C

Where:

df <- data.frame(
  a = c(1L, 2L, 1L, 4L, 5L, 3L, 3L), 
  b = c(4L, 3L, 5L, 1L, 1L, 2L, 2L), 
  c = c("A", "B", "C", "A", "C", "B", "E")
)
s_baldur
  • 29,441
  • 4
  • 36
  • 69