How do I get a contingency table?

后端 未结 6 1501
感动是毒
感动是毒 2020-11-21 23:27

I am trying to create a contingency table from a particular type of data. This would be doable with loops etc... but because my final table would contain more than 10E5 cell

6条回答
  •  无人共我
    2020-11-22 00:19

    With dplyr / tidyr:

    df <- read.table(text='PLANT                  ANIMAL                          INTERACTIONS
                     Tragopogon_pratensis   Propylea_quatuordecimpunctata         1
                     Anthriscus_sylvestris  Rhagonycha_nigriventris               3
                     Anthriscus_sylvestris  Sarcophaga_carnaria                   2
                     Heracleum_sphondylium  Sarcophaga_carnaria                   1
                     Anthriscus_sylvestris  Sarcophaga_variegata                  4
                     Anthriscus_sylvestris  Sphaerophoria_interrupta_Gruppe       3
                     Cerastium_holosteoides Sphaerophoria_interrupta_Gruppe       1', header=TRUE)
    library(dplyr)
    library(tidyr)
    df %>% spread(ANIMAL, INTERACTIONS, fill=0)
    
    #                    PLANT Propylea_quatuordecimpunctata Rhagonycha_nigriventris Sarcophaga_carnaria Sarcophaga_variegata Sphaerophoria_interrupta_Gruppe
    # 1  Anthriscus_sylvestris                             0                       3                   2                    4                               3
    # 2 Cerastium_holosteoides                             0                       0                   0                    0                               1
    # 3  Heracleum_sphondylium                             0                       0                   1                    0                               0
    # 4   Tragopogon_pratensis                             1                       0                   0                    0                               0
    

提交回复
热议问题