How do I get a contingency table?

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

    In base R, use table or xtabs:

    with(warpbreaks, table(wool, tension))
    
        tension
    wool L M H
       A 9 9 9
       B 9 9 9
    
    xtabs(~wool+tension, data=warpbreaks)
    
        tension
    wool L M H
       A 9 9 9
       B 9 9 9
    

    The gmodels packages has a function CrossTable that gives output similar to what users of SPSS or SAS expects:

    library(gmodels)
    with(warpbreaks, CrossTable(wool, tension))
    
    
       Cell Contents
    |-------------------------|
    |                       N |
    | Chi-square contribution |
    |           N / Row Total |
    |           N / Col Total |
    |         N / Table Total |
    |-------------------------|
    
    
    Total Observations in Table:  54 
    
    
                 | tension 
            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 |           | 
    -------------|-----------|-----------|-----------|-----------|
    

提交回复
热议问题