2017-05-29 32 views
1

我想建立一個應急表像一個病人數據庫是這樣的:應急表像R表示按行分組的數據

> 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" 

,並創建一個表就知道有多少病人有各健康狀態,按患者分組;結果是這樣的:

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

至於說,第一行是指2例有「ALCOOL」 healthstate,但只有一個同時具有「ALCOOL」和「病毒」 Healthstate。

「表」函數給了我這個結果,所以它不是我正在尋找的。

> table(data$Patient_nb, data$Healthstate) 

      Alcool Virus Autoimmune 
patient1  1  1   0 
patient2  1  0   0 
patient3  0  1   1 

回答

1

我們需要一個crossprod

crossprod(table(data))