I am struggling to count the number of unique combinations in my data. I would like to first group them by the id
and then count, how many times combination of each values occurs. here, it does not matter if the elements are combined in 'd-f
or f-d
, they still belongs in teh same category, as they have same element:
combinations:
n
c-f: 2 # aslo f-c
c-d-f: 1 # also cfd or fdc
d-f: 2 # also f-d or d-f. The dash is only for isualization purposes
Dummy example:
# my data
dd <- data.frame(id = c(1,1,2,2,2,3,3,4, 4, 5,5),
cat = c('c','f','c','d','f','c','f', 'd', 'f', 'f', 'd'))
> dd
id cat
1 1 c
2 1 f
3 2 c
4 2 d
5 2 f
6 3 c
7 3 f
8 4 d
9 4 f
10 5 f
11 5 d
Using paste
is a great solution provided by @benson23, but it considers as unique category f-d
and d-f
. I wish, however, that the order will not matter. Thank you!