'Remove duplicate combinations in R [duplicate]
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')
Solution 1:[1]
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")
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sindri_baldur |