[R] cross tabulation: convert frequencies to percentages
Marc Schwartz
marc_schwartz at comcast.net
Fri Feb 27 15:59:12 CET 2009
on 02/27/2009 08:43 AM soeren.vogel at eawag.ch wrote:
> Hello,
>
> might be rather easy for R pros, but I've been searching to the dead end
> to ...
>
> twsource.area <- table(twsource, area, useNA="ifany")
>
> gives me a nice cross tabulation of frequencies of two factors, but now
> I want to convert to pecentages of those absolute values. In addition
> I'd like an extra column and an extra row with absolute sums. I know,
> Excel or the likes will produce it more easily, but how would the
> procedure look like in R?
See ?prop.table which is referenced in the See Also section of ?table.
This will give you proportions, so if you want percentages, just
multiply by 100.
To add row and column totals, see ?addmargins which is also in the See
Also for ?table
TAB <- table(state.division, state.region)
> TAB
state.region
state.division Northeast South North Central West
New England 6 0 0 0
Middle Atlantic 3 0 0 0
South Atlantic 0 8 0 0
East South Central 0 4 0 0
West South Central 0 4 0 0
East North Central 0 0 5 0
West North Central 0 0 7 0
Mountain 0 0 0 8
Pacific 0 0 0 5
# Overall table proportions
> prop.table(TAB)
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
# Column proportions
> prop.table(TAB, 2)
state.region
state.division Northeast South North Central West
New England 0.6666667 0.0000000 0.0000000 0.0000000
Middle Atlantic 0.3333333 0.0000000 0.0000000 0.0000000
South Atlantic 0.0000000 0.5000000 0.0000000 0.0000000
East South Central 0.0000000 0.2500000 0.0000000 0.0000000
West South Central 0.0000000 0.2500000 0.0000000 0.0000000
East North Central 0.0000000 0.0000000 0.4166667 0.0000000
West North Central 0.0000000 0.0000000 0.5833333 0.0000000
Mountain 0.0000000 0.0000000 0.0000000 0.6153846
Pacific 0.0000000 0.0000000 0.0000000 0.3846154
> addmargins(TAB)
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
HTH,
Marc Schwartz
More information about the R-help
mailing list