这是我的列联表cxTable
:
Always Wrong Almst Always Wrg Sometimes Wrong Not Wrong At All
Lower Class 1344 166 146 73
Working Class 11997 1775 1105 355
Middle Class 11221 2320 1188 341
Upper Class 696 177 125 47
添加边距效果很好:
> marginCxTable = addmargins(cxTable)
> marginCxTable
Always Wrong Almst Always Wrg Sometimes Wrong Not Wrong At All Sum
Lower Class 1344 166 146 73 1729
Working Class 11997 1775 1105 355 15232
Middle Class 11221 2320 1188 341 15070
Upper Class 696 177 125 47 1045
Sum 25258 4438 2564 816 33076
然而,添加分数我的总和只有25%而不是100%。所有其他条目也除以4。
> prop.table(marginCxTable)
Always Wrong Almst Always Wrg Sometimes Wrong Not Wrong At All Sum
Lower Class 0.0101584230 0.0012546862 0.0011035192 0.0005517596 0.0130683880
Working Class 0.0906775305 0.0134160721 0.0083519773 0.0026832144 0.1151287943
Middle Class 0.0848122506 0.0175353731 0.0089793204 0.0025773975 0.1139043415
Upper Class 0.0052606119 0.0013378280 0.0009447938 0.0003552425 0.0078984762
Sum 0.1909088161 0.0335439594 0.0193796106 0.0061676140 0.2500000000
我可能错过了一些非常简单的事情。谢谢你的帮助。
答案 0 :(得分:1)
Per @joran的评论,你想在prop.table()
之前做addmargins()
。使用mtcars
数据进行说明,以便其他人可以重现输出:
mt <- table(mtcars$cyl, mtcars$am)
addmargins(prop.table(mt))
# 0 1 Sum
# 4 0.09375 0.25000 0.34375
# 6 0.12500 0.09375 0.21875
# 8 0.37500 0.06250 0.43750
# Sum 0.59375 0.40625 1.00000