Home > Software design >  Calculate associated events
Calculate associated events

Time:08-05

I have a dataset composed only of variables whose value is 1 and 0. 1 means the presence of a certain event, while 0 means the absence of it.

df <- data.frame(event1 = c(1, 0, 0, 1, 0, 0, 1),
                    event2 = c(1, 1, 0, 1, 0, 0, 1),
                    event3 = c(1, 0, 0, 0, 0, 0, 0))

I would like to have a matrix or heatmap which gives me the correlation between these events, that is, when more than one variables for the same record has the value of 1.

In the sample dataset I have above I should have event1 and event2 associated 3 times (first record, fourth record and last record), event2 and event3 associated 1 time (first record) and so on.

CodePudding user response:

You can do this with matrix multiplication:

m = as.matrix(df)
t(m) %*% m
#        event1 event2 event3
# event1      3      3      1
# event2      3      4      1
# event3      1      1      1
  • Related