I am trying to solve this problem in R. I have the following data:
item id
1 500
2 500
2 600
2 700
3 500
3 600
data.frame(item = c(1, 2, 2, 2, 3, 3),
id = c(500, 500, 600, 700, 500, 600))
And I want to count the number of cases when a pair of elements is associated with the same identifier. So I want this conclusion:
item1 item2 count
1 2 1
2 3 2
1 3 2
I tried to get closer to this with commands like:
x_agg = aggregate(x, by=list(x$id), c)
and then
x_agg_id = lapply(x_agg$item, unique)
thinking that I could count the appearance of each element. But the function byseems to create a list object, which I don't know how to manipulate. I hope there is an easier way ...
source
share