1

I'd like to build a contingency table-like for a patients database like this :

> data <- data.frame(Patient_nb = c("patient1", "patient1", "patient2", "patient3", "patient3"), Healthstate=c("Virus", "Alcool", "Alcool", "Virus", "Autoimmune"))

    Patient_nb   Healthstate   
 1   patient1     "Virus"
 2   patient1     "Alcool"
 3   patient2     "Alcool"
 4   patient3     "Virus"
 5   patient3     "Autoimmune"

And create a table to know how many patients have each healthstate, grouped by patients ; a result like this :

           Alcool    Virus   Autoimmune
Alcool       2         1         0
Virus        1         2         1
Autoimmune   0         1         1

As to say, the first row means that 2 patients have "Alcool" healthstate, but only one has both "Alcool" and "Virus" Healthstate.

The "table" function give me this result, so it is not what I'm searching for.

> table(data$Patient_nb, data$Healthstate)

           Alcool   Virus   Autoimmune
patient1     1        1         0
patient2     1        0         0
patient3     0        1         1
Dash
  • 28
  • 4

1 Answers1

1

We need a crossprod

crossprod(table(data))
akrun
  • 874,273
  • 37
  • 540
  • 662