Chapter 15. Getting Your Data into Shape
When it comes to making graphs, half the battle occurs before you call any graphing commands. Before you pass your data to the graphing functions, it must first be read in and given the correct structure. The data sets provided with R are ready to use, but when dealing with real-world data, this usually isn’t the case: you’ll have to clean up and restructure the data before you can visualize it.
Data sets in R are most often stored in data frames. They’re typically used as two-dimensional data structures, with each row representing one case and each column representing one variable. Data frames are essentially lists of vectors and factors, all of the same length, where each vector or factor represents one column.
Here’s the heightweight data
set:
library(gcookbook)# For the data setheightweightsex ageYear ageMonth heightIn weightLb f11.9214356.385.0f12.9215562.3105.0...m13.9216762.0107.5m12.5815159.387.0
It consists of five columns, with each row representing one case: a
set of information about a single person. We can get a clearer idea of how
it’s structured by using the str()
function:
str(heightweight)'data.frame':236obs. of5variables:$sex : Factor w/2levels"f","m":1111111111...$ageYear : num11.912.912.813.415.9...$ageMonth: int143155153161191171185142160140...$heightIn: num56.362.363.35962.562.55956.56253.8...$weightLb: num8510510892112...
The first column, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access