Create summary table of categorical variables of different lengths

后端 未结 6 658
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 11:04

In SPSS it is fairly easy to create a summary table of categorical variables using \"Custom Tables\":

\"This

6条回答
  •  没有蜡笔的小新
    2021-01-02 11:43

    One way to get the output, but not the formatting:

    library(plyr)
    ldply(mtcars[,8:11],function(x) t(rbind(names(table(x)),table(x),paste0(prop.table(table(x))*100,"%"))))
        .id 1  2       3
    1    vs 0 18  56.25%
    2    vs 1 14  43.75%
    3    am 0 19 59.375%
    4    am 1 13 40.625%
    5  gear 3 15 46.875%
    6  gear 4 12   37.5%
    7  gear 5  5 15.625%
    8  carb 1  7 21.875%
    9  carb 2 10  31.25%
    10 carb 3  3  9.375%
    11 carb 4 10  31.25%
    12 carb 6  1  3.125%
    13 carb 8  1  3.125%
    

提交回复
热议问题