table
및 melt
을 reshape2
패키지로 사용할 수 있습니다.
#DATA
df = structure(list(Name = c("John", "Anna", "Dave", "Stew", "John",
"Anna", "John", "Stew", "Dave"), Event = c("EventA", "EventA",
"EventA", "EventB", "EventB", "EventB", "EventC", "EventC", "EventC"
)), .Names = c("Name", "Event"), row.names = c(NA, -9L), class = "data.frame")
#Get Pairwise Frequency
a = table(df) %*% t(table(df))
a
# Name
#Name Anna Dave John Stew
# Anna 2 1 2 1
# Dave 1 2 2 1
# John 2 2 3 2
# Stew 1 1 2 2
#If you want, set diagonal elements to zero (From Karthik's comment)
#diag(a) <- 0
library(reshape2)
output = data.frame(melt(a))
colnames(output) = c("Name1", "Name2", "Value")
#Remove the pair with oneself
output = output[-(which(output$Name1 == output$Name2)),]
output
# Name1 Name2 Value
#2 Dave Anna 1
#3 John Anna 2
#4 Stew Anna 1
#5 Anna Dave 1
#7 John Dave 2
#8 Stew Dave 1
#9 Anna John 2
#10 Dave John 2
#12 Stew John 2
#13 Anna Stew 1
#14 Dave Stew 1
#15 John Stew 2
#YOU CAN PASTE 'NAME1' and 'NAME2' to a 'PAIR' if necessary
#output$PAIR = apply(output, 1, function(x) paste(sort(x[1:2]), collapse = " "))
첫 번째 표에서 John-John 항목이 왜 0입니까? 왜 3이 아닙니까? – G5W
다른 쌍의 빈도에만 관심이 있습니다. 존 - 존이 3인데 반해, 나는 짝을 이루고 싶지 않습니다. – Mark