1

I need to convert an occurrence matrix (in a file, say infile.txt) to a co-occurrence matrix as below. Is there any inbuilt function in r to do the same. I can do this using C style programming but I am sure there must be some function to perform the same.

This is my occurrence matrix similar to transaction matrix. A 0 represent non occurrence while 1 represent an occurence of an event.

a   b   c   d   
0   1   0   1   
0   1   1   1   
1   0   0   1   
1   1   1   0   
1   0   0   0   

Co-occurrence can be summarized by checking how many times a pair of events occurred together. This can be found by counting any two columns together by counting how many times both column had 1.

    a   b   c   d
a   0   1   1   1
b   1   0   2   2
c   1   2   0   1
d   1   2   1   0

Edit: As pointed out by Jiber, a similar question is here: Create a co-occurrence matrix from dummy-coded observations

Cœur
  • 37,241
  • 25
  • 195
  • 267
learner
  • 2,582
  • 9
  • 43
  • 54

1 Answers1

0

Use crossprod:

dt<-as.matrix(read.table(text="a   b   c   d   
0   1   0   1   
0   1   1   1   
1   0   0   1   
1   1   1   0   
1   0   0   0 ",header=T))

x<-crossprod(dt)
diag(x)<-0
x
##   a b c d
## a 0 1 1 1
## b 1 0 2 2
## c 1 2 0 1
## d 1 2 1 0
mrip
  • 14,913
  • 4
  • 40
  • 58