I have a binary matrix:
S1 S2 S3 S4 S5 D1 D2 D3 D4
obs1 0 0 0 1 0 0 1 0 0
obs2 0 1 0 0 0 1 0 0 0
obs3 0 0 1 0 0 0 0 1 0
obs4 0 0 0 1 0 0 1 0 0
obs5 0 1 0 0 0 0 1 0 0
obs6 0 0 0 1 0 0 1 0 0
Each row of the matrix must contain the value 1 for the S group (S1, S2, S3, S4 or S5) and the value 1 for D group (D1, D2, D3 or D4).
How can I create a contingency table for the two groups based on two columns of the binary matrix where the value 1 appears? i.e., I would like to have this table format:
D1 D2 D3 D4
S1 0 0 0 0
S2 1 1 0 0
S3 0 0 1 0
S4 0 3 0 0
S5 0 0 0 0
Here, for example, the value 3 is coming from obs1, obs4 and obs6, where the couple (S4,D2) take simultaniously 1 as value.