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
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))