After looking through the R Markdown gallery, I chose an HTML document. I liked being able to keep the code, results, and a chart on the same page. The linked table of contents also makes it easy to jump between sections, and the report can be shared through RPubs.
I used the mtcars dataset to explore a simple question:
how does fuel economy differ across cylinder groups? From a marketing
perspective, fuel economy is a product feature that could matter to
buyers who care about running costs.
The data comes from R’s datasets package and covers 32
cars from the 1973–74 model years, using information from the 1974
Motor Trend magazine. I exported the original dataset to
mtcars.csv, keeping the model names in a separate column,
and read that file below. The numerical values are unchanged.
cars <- read.csv("mtcars.csv", stringsAsFactors = FALSE)
nrow(cars)
## [1] 32
sum(is.na(cars))
## [1] 0
head(cars[, c("model", "mpg", "cyl", "hp", "wt")])
## model mpg cyl hp wt
## 1 Mazda RX4 21.0 6 110 2.620
## 2 Mazda RX4 Wag 21.0 6 110 2.875
## 3 Datsun 710 22.8 4 93 2.320
## 4 Hornet 4 Drive 21.4 6 110 3.215
## 5 Hornet Sportabout 18.7 8 175 3.440
## 6 Valiant 18.1 6 105 3.460
There are 32 cars and 0 missing values. The main variables I use are
fuel economy (mpg, miles per US gallon), horsepower
(hp), and weight (wt, measured in thousands of
pounds). Higher mpg means a car travels farther on a gallon of fuel.
measures <- cars[, c("mpg", "hp", "wt")]
summary_stats <- data.frame(
Variable = c("Fuel economy (mpg)", "Horsepower (hp)",
"Weight (1,000 lbs)"),
Mean = sapply(measures, mean),
Median = sapply(measures, median),
SD = sapply(measures, sd),
Minimum = sapply(measures, min),
Maximum = sapply(measures, max)
)
knitr::kable(summary_stats, digits = 2, row.names = FALSE,
caption = "Summary statistics for the 32 cars")
| Variable | Mean | Median | SD | Minimum | Maximum |
|---|---|---|---|---|---|
| Fuel economy (mpg) | 20.09 | 19.20 | 6.03 | 10.40 | 33.90 |
| Horsepower (hp) | 146.69 | 123.00 | 68.56 | 52.00 | 335.00 |
| Weight (1,000 lbs) | 3.22 | 3.33 | 0.98 | 1.51 | 5.42 |
Average fuel economy is 20.09 mpg, compared with a median of 19.20 mpg. Fuel economy ranges from 10.4 to 33.9 mpg, so the overall average hides quite a bit of variation. SD is the sample standard deviation; for fuel economy, it is 6.03 mpg.
group_counts <- table(cars$cyl)
group_means <- tapply(cars$mpg, cars$cyl, mean)
group_summary <- data.frame(
Cylinders = as.integer(names(group_counts)),
Cars = as.integer(group_counts),
Mean_mpg = as.numeric(group_means)
)
knitr::kable(group_summary, digits = 2, row.names = FALSE,
col.names = c("Cylinders", "Number of cars", "Mean mpg"))
| Cylinders | Number of cars | Mean mpg |
|---|---|---|
| 4 | 11 | 26.66 |
| 6 | 7 | 19.74 |
| 8 | 14 | 15.10 |
bar_positions <- barplot(
group_summary$Mean_mpg,
names.arg = paste(group_summary$Cylinders, "cylinders"),
col = c("#437F97", "#71A6A0", "#ADC9B0"),
border = NA,
ylim = c(0, 32),
ylab = "Mean fuel economy (miles per US gallon)",
main = "Average fuel economy by cylinder count"
)
text(bar_positions, group_summary$Mean_mpg,
labels = sprintf("%.2f", group_summary$Mean_mpg), pos = 3)
The four-cylinder cars average 26.66 mpg, while the eight-cylinder cars average 15.10 mpg. That is a difference of about 11.56 mpg. The six-cylinder group falls in between at 19.74 mpg.
The chart made that difference easier to see than the overall summary alone. For a marketing report, I would use a comparison like this to explain a product attribute clearly. However, these are older cars from a small sample, so I would not use the results to make claims about today’s car market. The comparison also does not show that cylinder count alone causes the difference in fuel economy; the cars differ in other ways, including weight and horsepower.