I am trying to find the number of groups in a data frame that contain specific pairs. Here is an example of what I have done and the desired output.
Creating the data
df=data.frame(c("Sam","Sam","Sam","Jason", "Jason", "Kelly", "Kelly"),
c("e","f","g","h", "h", "e", "f"))
names(df)=c('name','value')
Not interested in looking at the pairs which do not occur within at least one specific name, so I drop those observations before generating the pairs
df=df[!duplicated(df[1:2]),]
df=df[ave(rep(1, nrow(df)), df$name, FUN=length)>1,]
pairs=t(combn(unique(df$value), 2))
Now I have two objects that look like this
name value
1 Sam e
2 Sam f
3 Sam g
6 Kelly e
7 Kelly f
[,1] [,2]
[1,] e f
[2,] e g
[3,] f g
My Desired Output
pair.1 pair.2 occurrences
1 e f 2
2 e g 1
3 f g 1