I have a list of ids and places where these ids have been. Now I want to find pairs of ids that have most places in common.
My data frame looks like this:
You may try
library(reshape2)
tbl <- crossprod(table(df1[2:1]))
tbl[upper.tri(tbl, diag=TRUE)] <- 0
res <- subset(melt(tbl), value!=0)
colnames(res) <- c(paste0('pair',1:2), 'count')
row.names(res) <- NULL
res
# pair1 pair2 count
#1 Joe Dave 1
#2 Stuart Dave 3
#3 Stuart Joe 2
Or another option is
Subdf <- subset(merge(df1, df1, by.x='place',
by.y='place'), id.x!=id.y)
Subdf[-1] <- t(apply(Subdf[-1], 1, sort))
aggregate(place~., unique(Subdf), FUN=length)
# id.x id.y place
#1 Dave Joe 1
#2 Dave Stuart 3
#3 Joe Stuart 2