How do I get a contingency table?

后端 未结 6 1499
感动是毒
感动是毒 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:00

    the reshape package should do the trick.

    > library(reshape)
    
    > df <- data.frame(PLANT = c("Tragopogon_pratensis","Anthriscus_sylvestris","Anthriscus_sylvestris","Heracleum_sphondylium","Anthriscus_sylvestris","Anthriscus_sylvestris","Cerastium_holosteoides"),
                       ANIMAL= c("Propylea_quatuordecimpunctata","Rhagonycha_nigriventris","Sarcophaga_carnaria","Sarcophaga_carnaria","Sarcophaga_variegata","Sphaerophoria_interrupta_Gruppe","Sphaerophoria_interrupta_Gruppe"),
                       INTERACTIONS = c(1,3,2,1,4,3,1),
                       stringsAsFactors=FALSE)
    
    > df <- melt(df,id.vars=c("PLANT","ANIMAL"))    
    > df <- cast(df,formula=PLANT~ANIMAL)
    > df <- replace(df,is.na(df),0)
    
    > df
                       PLANT Propylea_quatuordecimpunctata Rhagonycha_nigriventris
    1  Anthriscus_sylvestris                             0                       3
    2 Cerastium_holosteoides                             0                       0
    3  Heracleum_sphondylium                             0                       0
    4   Tragopogon_pratensis                             1                       0
      Sarcophaga_carnaria Sarcophaga_variegata Sphaerophoria_interrupta_Gruppe
    1                   2                    4                               3
    2                   0                    0                               1
    3                   1                    0                               0
    4                   0                    0                               0
    

    I'm still figuring out how to fix the order issue, any suggestion?

提交回复
热议问题