r - Creation of a contingency table based on two columns of a binary matrix -


this question has answer here:

i have 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 matrix must contain value 1 s group (s1, s2, s3, s4 or s5) , value 1 d group (d1, d2, d3 or d4).

how can create contingency table 2 groups based on 2 columns of binary matrix value 1 appears? i.e., have 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, example, value 3 coming obs1, obs4 , obs6, couple (s4,d2) take simultaniously 1 value.

a nested apply

 t(apply(df[2:6], 2, function(x) apply(df[7:10], 2, function(y) sum(x==1 & y ==1))))    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 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -