Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

### Problem

How can I convert a frequency table into proportions?

### Context

This is a continuation of the data manipulation discussed in the ​​with() post. I had just finished making a table

# Load data from GitHub

# Two-way table by pool and revetment with(polygon, table(revetment, pool))

What if I want to see this table broken down by proportion of polygons, not counts?

### Solution

The prop.table() function will do this nicely.

library(dplyr)
prop <- with(polygon, table(revetment, pool)) %>% prop.table()
prop

By default, the proportions are calculated over the entire table. So each cell represents the proportion of all polygons that are in that pool with that value of revetment. The whole table sums to 1.

If you want proportions across rows or down columns, all you need to do is add the margin =  argument.

margin = 1 sums across rows. Each row sums to 1. This would answer the question, “What proportion of the polygons [with, or without] revetment are located in each of the three pools?”

prop.1 <- with(polygon, table(revetment, pool)) %>%
prop.table(margin = 1)
prop.1

margin = 2 sums down columns. Each column sums to 1. This would answer the question, “What proportion of the polygons in [pool] have revetment? (or, what proportion don’t have revetment?)

prop.2 <- with(polygon, table(revetment, pool)) %>%
prop.table(margin = 2)
prop.2

### Outcome

Handy function for creating proportion tables.