How do I get a contingency table?

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

    xtabs in base R should work, for example:

    dat <- data.frame(PLANT = c("p1", "p2", "p2", "p4", "p5", "p5", "p6"),
                      ANIMAL = c("a1", "a2", "a3", "a3", "a4", "a5", "a5"),
                      INTERACTIONS = c(1,3,2,1,4,3,1),
                      stringsAsFactors = FALSE)
    
    (x2.table <- xtabs(dat$INTERACTIONS ~ dat$PLANT + dat$ANIMAL))
    
         dat$ANIMAL
    dat$PLANT a1 a2 a3 a4 a5
           p1  1  0  0  0  0
           p2  0  3  2  0  0
           p4  0  0  1  0  0
           p5  0  0  0  4  3
           p6  0  0  0  0  1
    
    chisq.test(x2.table, simulate.p.value = TRUE)
    

    I think that should do what you're looking for fairly easily. I'm not sure how it scales up in terms of efficiency to a 10E5 contingency table, but that might be a separate issue statistically.

提交回复
热议问题