7Tables
The alternative to using graphics is to summarise your data in tabular form. Broadly speaking, if you want to convey detail, use a table, and if you want to show effects, then use graphics. You are more likely to want to use a table to summarise data when your data are categorical (such as people's names, or different commodities) than when they are continuous (in which case a scatterplot is likely to be more informative; see Section 5.1).
7.1 Tabulating categorical or discrete data
We'll first look at tables for categorical data, or discrete (numeric) data with a very limited number of unique values. Our main tools will be the following functions:
table ()
: Produces a table of counts;prop.table ()
: Produces a table of proportions.
7.1.1 Tables of counts
The table ()
function is perhaps the most useful of all the simple vector functions, because it does so much work behind the scenes. We have a vector of objects (they could be numbers or character strings), and we want to know how many of each is present in the vector.
Suppose we generate 1000 integers from a Poisson distribution with mean 0.6. We want to count up all of the zeros, ones, twos, and so on. A big task, but here is the table ()
function in action:
The function works for characters as well as for numbers and for multiple classifying variables. Let us take the dataset disease
which contains information ...
Get The R Book, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.