Converting R contingency tables to data frames

August 11, 2010

(This article was first published on Computational Ecology, and kindly contributed to R-bloggers)

A contingency table presents the joint density of one or more
categorical variables. Each entry in a contingency table is a count
of the number of times a particular set of factors levels occurs in
the dataset. For example, consider a list of plant species where
each species is assigned a relative seed size (small, medium, or
large) and a growth form (tree, shrub, or herb).

seed.sizes <- c("small", "medium", "large")
growth.forms <- c("tree", "shrub", "herb")
species.traits <- data.frame(
  seed.size = seed.sizes[c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3)],
  growth.form = growth.forms[c(3, 3, 2, 2, 1, 2, 2, 3, 1, 1, 1, 1)]
seed.size growth.form
small herb
small herb
small shrub
small shrub
small tree
medium shrub
medium shrub
medium herb
medium tree
large tree
large tree
large tree

A contingency table will tell us how many times each combination of
seeds.sizes and growth.forms occur.

tbl <- table(species.traits)
herb shrub tree
0 0 3
1 2 1
2 2 1

The output contingency table are of class table. The behaviour of
these objects is not quite like a data frame. In fact, trying to
convert them to a data frame gives a non-intuitive result.
seed.size growth.form Freq
large herb 0
medium herb 1
small herb 2
large shrub 0
medium shrub 2
small shrub 2
large tree 3
medium tree 1
small tree 1

Coercion of the table into a data frame puts each factor of the
contingency table into its own column along with the frequency,
rather than keeping the same structure as original table object.
If we wanted to turn the table into a data frame keeping the
original structure we use This function is
not well-documented in R, and this is probably the only situation in
which it would be used. But, it works.
herb shrub tree
0 0 3
1 2 1
2 2 1

To leave a comment for the author, please follow the link and comment on their blog: Computational Ecology. 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...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training



CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)