I'm trying to use ddply
(a plyr
function) to sort and identify the most frequent interaction type between any unique pairs of user from a social media data of the following form
from <- c('A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D')
to <- c('B', 'B', 'D', 'A', 'C', 'C', 'D', 'A', 'D', 'B', 'A', 'B', 'B', 'A', 'C')
interaction_type <- c('like', 'comment', 'share', 'like', 'like', 'like', 'comment', 'like', 'like', 'share', 'like', 'comment', 'like', 'share', 'like')
dat <- data.frame(from, to, interaction_type)
which, if aggregate correctly, should find the most common type of interaction between any unique pairs (regardless of directionality (i.e., A-->B, A<--B)) like this
from to type
A B like
A C like
A D share
B C like
B D comment
C D like
While it's easy to get the total count of interaction between any two users by using
count <- ddply(sub_test, .(from, to), nrow)
I found it hard to apply similar method to find the most common type of interaction between any given pairs with this aggregation method. What will be the most efficient way to achieve my desired output? Also, how to handle possible "tied" cases? (I might just use "tided" as the cell values for all tied cases).