**woodpeckR**, and kindly contributed to R-bloggers)

### 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 polygon <- read.csv("https://raw.githubusercontent.com/kaijagahm/general/master/polygon_sampling_data_UMR.csv") # 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.

**leave a comment**for the author, please follow the link and comment on their blog:

**woodpeckR**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...