2010-08-11

Converting R contingency tables to data frames

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.sizegrowth.form
smallherb
smallherb
smallshrub
smallshrub
smalltree
mediumshrub
mediumshrub
mediumherb
mediumtree
largetree
largetree
largetree

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

tbl <- table(species.traits)
herbshrubtree
003
121
221

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.

as.data.frame(tbl)
seed.sizegrowth.formFreq
largeherb0
mediumherb1
smallherb2
largeshrub0
mediumshrub2
smallshrub2
largetree3
mediumtree1
smalltree1

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 as.data.frame.matrix. This function is not well-documented in R, and this is probably the only situation in which it would be used. But, it works.

as.data.frame.matrix(tbl)
herbshrubtree
003
121
221

Labels:

253 Comments:

«Oldest ‹Older 401 – 253 of 253 «Oldest ‹Older 401 – 253 of 253

Post a Comment

Subscribe to Post Comments [Atom]

<< Home