My data represents the last two purchases for each member and are ordered by FROM -> TO
df <- data.frame(member_id = c(1,1,2,2,3,3,4,4,5,5), direction = c('FROM','TO','FROM','TO','FROM','TO','FROM','TO','FROM','TO'),product = c('A','A','A','B','C','D','C','C','A','A'))
I am trying to build a transition matrix to represent how members move between products.
Based on the data above I hope to populate the matrix with the count of events from -> to as follows:
A B C D
A 2 1 0 0
B 0 0 0 0
C 0 0 1 1
D 0 0 0 0
How can I achieve this using dplyr syntax?