How do I get a contingency table?

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

    I'd like to point out that we can get the same results Andrie posted without using the function with:

    R Base Package

    # 3 options
    table(warpbreaks[, 2:3])
    table(warpbreaks[, c("wool", "tension")])
    table(warpbreaks$wool, warpbreaks$tension, dnn = c("wool", "tension"))
    
        tension
    wool L M H
       A 9 9 9
       B 9 9 9
    

    Package gmodels:

    library(gmodels)
    # 2 options    
    CrossTable(warpbreaks$wool, warpbreaks$tension)
    CrossTable(warpbreaks$wool, warpbreaks$tension, dnn = c("Wool", "Tension"))
    
    
       Cell Contents
    |-------------------------|
    |                       N |
    | Chi-square contribution |
    |           N / Row Total |
    |           N / Col Total |
    |         N / Table Total |
    |-------------------------|
    
    
    Total Observations in Table:  54 
    
    
                    | warpbreaks$tension 
    warpbreaks$wool |         L |         M |         H | Row Total | 
    ----------------|-----------|-----------|-----------|-----------|
                  A |         9 |         9 |         9 |        27 | 
                    |     0.000 |     0.000 |     0.000 |           | 
                    |     0.333 |     0.333 |     0.333 |     0.500 | 
                    |     0.500 |     0.500 |     0.500 |           | 
                    |     0.167 |     0.167 |     0.167 |           | 
    ----------------|-----------|-----------|-----------|-----------|
                  B |         9 |         9 |         9 |        27 | 
                    |     0.000 |     0.000 |     0.000 |           | 
                    |     0.333 |     0.333 |     0.333 |     0.500 | 
                    |     0.500 |     0.500 |     0.500 |           | 
                    |     0.167 |     0.167 |     0.167 |           | 
    ----------------|-----------|-----------|-----------|-----------|
       Column Total |        18 |        18 |        18 |        54 | 
                    |     0.333 |     0.333 |     0.333 |           | 
    ----------------|-----------|-----------|-----------|-----------|
    

提交回复
热议问题