marginal total in tables

若如初见. 提交于 2019-12-02 17:32:42

To add margins, use addmargins()

addmargins(table(state.division, state.region))
                    state.region
state.division       Northeast South North Central West Sum
  New England                6     0             0    0   6
  Middle Atlantic            3     0             0    0   3
  South Atlantic             0     8             0    0   8
  East South Central         0     4             0    0   4
  West South Central         0     4             0    0   4
  East North Central         0     0             5    0   5
  West North Central         0     0             7    0   7
  Mountain                   0     0             0    8   8
  Pacific                    0     0             0    5   5
  Sum                        9    16            12   13  50

To calculate percentages, use prop.table()

prop.table(table(state.division, state.region))
                    state.region
state.division       Northeast South North Central West
  New England             0.12  0.00          0.00 0.00
  Middle Atlantic         0.06  0.00          0.00 0.00
  South Atlantic          0.00  0.16          0.00 0.00
  East South Central      0.00  0.08          0.00 0.00
  West South Central      0.00  0.08          0.00 0.00
  East North Central      0.00  0.00          0.10 0.00
  West North Central      0.00  0.00          0.14 0.00
  Mountain                0.00  0.00          0.00 0.16
  Pacific                 0.00  0.00          0.00 0.10

It also works with survey package's svytable().

addmargins(svytable(formula = ~x1+x2, design = df.w))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!