Because State
comes first in the data frame, table
will use that as the row ID. Thus, you can divide the results of table
by the row sums to get ratios, or scale to percentage.
The table:
> table(x)
Ideology
State Conservative Independent Liberal
CO 2 1 3
DC 1 1 1
Using prop.table
to do the scaling, to get values per-state:
> prop.table(table(x), 1)
Ideology
State Conservative Independent Liberal
CO 0.3333333 0.1666667 0.5000000
DC 0.3333333 0.3333333 0.3333333
This is equivalent to table(x)/rowSums(table(x))
You can multiply by 100 to get percent values if needed.