Hi guys I have a question regarding an matrix operation in R. I have a data set like the one bellow:
Sample Data:
d <- data.frame(id=c(2,3,4,5,6,6,8,11,11,11,12,12,12),author=c("FN","VM","VA","FK","VM","SM","FK","FK","VB","VA","FK","VB","VA"))
d
id author
1 2 FN
2 3 VM
3 4 VA
4 5 FK
5 6 VM
6 6 SM
7 8 FK
8 11 FK
9 11 VB
10 11 VA
11 12 FK
12 12 VB
13 12 VA
1)Created an Incidence Matrix:
> m <- xtabs(~author+id,d)
> m
id
author 2 3 4 5 6 8 11 12
FK 0 0 0 1 0 1 1 1
FN 1 0 0 0 0 0 0 0
SM 0 0 0 0 1 0 0 0
VA 0 0 1 0 0 0 1 1
VB 0 0 0 0 0 0 1 1
VM 0 1 0 0 1 0 0 0
What I want to do is to generate pair combinations from the author list, in column 2, by multiplying each row. For instance for the pair FK-VA, its corresponding rows in the incidence matrix are this:
FK 0 0 0 1 0 1 1 1
VA 0 0 1 0 0 0 1 1
The expected outcome in my matrix should produce a multiplication by each element of the rows:
FK-VA (0*0),(0*0),(0*1),(1*0),(0*0),(1*0),(1*1),(1*1)
FK-VA 0 0 0 0 0 1 1
2)Expected outcome would be this matrix(m):
FK FN 0 0 0 0 0 0 0
FK SM 0 0 0 0 0 0 0
FK VA 0 0 0 0 0 1 1
FK VB 0 0 0 0 0 1 1
FK VM 0 0 0 0 0 0 0
FN SM 0 0 0 0 0 0 0
FN VA 0 0 0 0 0 0 0
FN VB 0 0 0 0 0 0 0
FN VM 0 0 0 0 0 0 0
SM VA 0 0 0 0 0 0 0
SM VB 0 0 0 0 0 0 0
SM VM 0 0 0 1 0 0 0
VA VB 0 0 0 0 0 1 1
VA VM 0 0 0 0 0 0 0
VB VM 0 0 0 0 0 0 0
3) Delete Empty rows.
As you can see I need help for the steps 2 and 3.
Thank you
Mario