Reputation: 6639
I have a data frame and I want to calculate the number of times each combination of events in two columns occur (in any order).
For example say I have
df <- data.frame('x' = c('a', 'a', 'b', 'c', 'c', 'c'),
'y' = c('b', 'c', 'c', 'a', 'a', 'b'))
So
x y
a b
a c
b c
c a
c a
c a
c b
a
and b
occur together once (1st row), a
and c
4 times (rows 2, 4, 5, 6) and b
and c
twice (3rd and 7th rows) so I would want to return
x-y num
a-b 1
a-c 4
b-c 2
I hope this makes sense? Thanks in advance
Upvotes: 2
Views: 2651
Reputation: 3210
This should work:
table(apply(df,1,function(x) paste(sort(x),collapse='-')))
a-b a-c b-c
1 3 2
Upvotes: 4