Getting both column counts and proportions in the same table in R

后端 未结 3 1636
礼貌的吻别
礼貌的吻别 2020-12-14 13:19

If there a function that will give me both counts and column/overall percents in the same table? I can looked at both tables and reshape2 and don\'t see an option for doing

3条回答
  •  旧巷少年郎
    2020-12-14 14:04

    Use CrossTable function from gmodles package.

    library(gmodels)
    

    Check the arguments of CrossTable

    args(CrossTable)
    function (x, y, digits = 3, max.width = 5, expected = FALSE, 
        prop.r = TRUE, prop.c = TRUE, prop.t = TRUE, prop.chisq = TRUE, 
        chisq = FALSE, fisher = FALSE, mcnemar = FALSE, resid = FALSE, 
        sresid = FALSE, asresid = FALSE, missing.include = FALSE, 
        format = c("SAS", "SPSS"), dnn = NULL, ...) 
    NULL
    

    Apply CrossTable

    CrossTable(x=d$x, y=d$y)
    
    
    
       Cell Contents
    |-------------------------|
    |                       N |
    | Chi-square contribution |
    |           N / Row Total |
    |           N / Col Total |
    |         N / Table Total |
    |-------------------------|
    
    
    Total Observations in Table:  100 
    
    
                 | d$y 
             d$x |         a |         b |         c | Row Total | 
    -------------|-----------|-----------|-----------|-----------|
               a |        13 |        12 |         8 |        33 | 
                 |     0.182 |     0.306 |     0.924 |           | 
                 |     0.394 |     0.364 |     0.242 |     0.330 | 
                 |     0.371 |     0.387 |     0.235 |           | 
                 |     0.130 |     0.120 |     0.080 |           | 
    -------------|-----------|-----------|-----------|-----------|
               b |        13 |        11 |        18 |        42 | 
                 |     0.197 |     0.313 |     0.969 |           | 
                 |     0.310 |     0.262 |     0.429 |     0.420 | 
                 |     0.371 |     0.355 |     0.529 |           | 
                 |     0.130 |     0.110 |     0.180 |           | 
    -------------|-----------|-----------|-----------|-----------|
               c |         9 |         8 |         8 |        25 | 
                 |     0.007 |     0.008 |     0.029 |           | 
                 |     0.360 |     0.320 |     0.320 |     0.250 | 
                 |     0.257 |     0.258 |     0.235 |           | 
                 |     0.090 |     0.080 |     0.080 |           | 
    -------------|-----------|-----------|-----------|-----------|
    Column Total |        35 |        31 |        34 |       100 | 
                 |     0.350 |     0.310 |     0.340 |           | 
    -------------|-----------|-----------|-----------|-----------|
    

提交回复
热议问题