R Data Set
A data set is a collection of data, usually organised as a table: each row is an observation (a car, a patient, a day) and each column is a variable measured on it. In R, a data set is normally stored in a data frame.
R comes with over 100 built-in data sets in the datasets package. They are perfect for practising because they are always available. In this lesson we explore mtcars, which describes 32 cars tested by Motor Trend magazine in 1974.
head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Size and variables
dim(mtcars)
names(mtcars)[1] 32 11
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
The 11 columns are:
| Column | Meaning |
|---|---|
mpg |
miles per (US) gallon |
cyl |
number of cylinders |
disp |
displacement (cubic inches) |
hp |
gross horsepower |
drat |
rear axle ratio |
wt |
weight (1000 lbs) |
qsec |
quarter-mile time (seconds) |
vs |
engine shape (0 = V-shaped, 1 = straight) |
am |
transmission (0 = automatic, 1 = manual) |
gear |
number of forward gears |
carb |
number of carburettors |
Structure and row names
str() shows the type of each column. The car names are not a column: they are the row names of the data frame:
str(mtcars[, 1:5])
rownames(mtcars)[1:5]
mtcars["Fiat 128", c("mpg", "hp")]'data.frame': 32 obs. of 5 variables:
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$ disp: num 160 160 108 258 360 ...
$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout"
mpg hp
Fiat 128 32.4 66
Summarising every column
summary() on a data frame summarises each column:
summary(mtcars[, c("mpg", "hp", "wt")]) mpg hp wt
Min. :10.40 Min. : 52.0 Min. :1.513
1st Qu.:15.43 1st Qu.: 96.5 1st Qu.:2.581
Median :19.20 Median :123.0 Median :3.325
Mean :20.09 Mean :146.7 Mean :3.217
3rd Qu.:22.80 3rd Qu.:180.0 3rd Qu.:3.610
Max. :33.90 Max. :335.0 Max. :5.424
Finding the data sets
On your own machine, data() lists every built-in data set and ?mtcars opens its documentation. A few you'll meet in this course:
| Data set | Description |
|---|---|
mtcars |
32 cars, 11 numeric variables |
iris |
150 iris flowers, 4 measurements + species |
airquality |
New York air quality, May–Sep 1973 (contains NAs) |
ToothGrowth |
guinea pig tooth length by vitamin C dose and supplement |
PlantGrowth |
plant weights under a control and two treatments |
women |
heights and weights of 15 women |
cars |
speed and stopping distance of 50 cars |
str(airquality)
colSums(is.na(airquality))'data.frame': 153 obs. of 6 variables:
$ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
$ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
$ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
$ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
$ Month : int 5 5 5 5 5 5 5 5 5 5 ...
$ Day : int 1 2 3 4 5 6 7 8 9 10 ...
Ozone Solar.R Wind Temp Month Day
37 7 0 0 0 0
airquality has missing values in Ozone and Solar.R. Real data almost always has gaps; the Missing Values lesson shows how to deal with them.
Test yourself
In a tidy data set, what does each row represent?
- A variable
- An observation
- A data type
- A summary
How many rows does mtcars have?
- 11
- 32
- 50
- 150
Where are the car names stored in mtcars?
- In the
namecolumn - In the row names
- In
levels() - They are not stored