marginal total in tables

后端 未结 2 640
再見小時候
再見小時候 2021-01-31 16:39

I have a data frame with a number of infections identified from clinical isolates at different dates.

So far I have organised the data into a shape that I want to start

相关标签:
2条回答
  • 2021-01-31 16:45

    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
    
    0 讨论(0)
  • 2021-01-31 16:48

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

    addmargins(svytable(formula = ~x1+x2, design = df.w))
    
    0 讨论(0)
提交回复
热议问题