Tables

Author

Jeffrey R. Stevens

Published

May 3, 2023

  1. Using the mpg data, create a data frame called my_mpg that capitalizes the manufacturer.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
my_mpg <- mpg |> 
  mutate(manufacturer = str_to_sentence(manufacturer))
  1. Calculate mean highway and city fuel efficiency for each manufacturer and return a table with the caption “Highway and city fuel efficiency” and column names “Manufacturer”, “Highway”, and “City”.
library(knitr)
my_mpg |> 
  summarise(mean_hwy = mean(hwy), mean_cty = mean(cty), .by = c(manufacturer)) |> 
  kable(digits = 1, 
        col.names = c("Manufacturer", "Highway", "City"),
        caption = "Highway and city fuel efficiency")
Highway and city fuel efficiency
Manufacturer Highway City
Audi 26.4 17.6
Chevrolet 21.9 15.0
Dodge 17.9 13.1
Ford 19.4 14.0
Honda 32.6 24.4
Hyundai 26.9 18.6
Jeep 17.6 13.5
Land rover 16.5 11.5
Lincoln 17.0 11.3
Mercury 18.0 13.2
Nissan 24.6 18.1
Pontiac 26.4 17.0
Subaru 25.6 19.3
Toyota 24.9 18.5
Volkswagen 29.2 20.9
  1. Repeat the table from #2 but add a header that spans Highway and City that says “Fuel efficiency”. Reminder, you’ll need to load {kableExtra} to do this.
library(kableExtra)
my_mpg |> 
  summarise(mean_hwy = mean(hwy), mean_cty = mean(cty), .by = c(manufacturer)) |> 
  kable(digits = 1, 
        col.names = c("Manufacturer", "Highway", "City"),
        caption = "Highway and city fuel efficiency") |> 
  add_header_above(c(" " = 1, "Fuel efficiency" = 2))
Highway and city fuel efficiency
Fuel efficiency
Manufacturer Highway City
Audi 26.4 17.6
Chevrolet 21.9 15.0
Dodge 17.9 13.1
Ford 19.4 14.0
Honda 32.6 24.4
Hyundai 26.9 18.6
Jeep 17.6 13.5
Land rover 16.5 11.5
Lincoln 17.0 11.3
Mercury 18.0 13.2
Nissan 24.6 18.1
Pontiac 26.4 17.0
Subaru 25.6 19.3
Toyota 24.9 18.5
Volkswagen 29.2 20.9
  1. Calculate mean highway and city fuel efficiency for each manufacturer and year. Order the data frame by year, then remove the year column and add labels for each year that spans the rows for each year. Also, add a footnote that says “Source: mpg data set.”.
my_mpg |> 
  summarise(mean_hwy = mean(hwy), mean_cty = mean(cty), .by = c(year, manufacturer)) |> 
  arrange(year) |> 
  select(-year) |> 
  kable(digits = 1, 
        col.names = c("Manufacturer", "Highway", "City"),
        caption = "Highway and city fuel efficiency") |> 
  pack_rows("1999", 1, 15) |> 
  pack_rows("2008", 16, 30) |> 
  footnote(general = "Source: mpg data set.")
Highway and city fuel efficiency
Manufacturer Highway City
1999
Audi 26.1 17.1
Chevrolet 21.6 15.1
Dodge 18.4 13.4
Ford 18.6 13.9
Honda 31.6 24.8
Hyundai 26.7 18.3
Jeep 18.5 14.5
Land rover 15.0 11.0
Lincoln 16.5 11.0
Mercury 17.0 13.5
Nissan 23.5 17.7
Pontiac 26.3 17.0
Subaru 25.5 19.0
Toyota 24.0 18.1
Volkswagen 29.7 21.2
2008
Audi 26.8 18.1
Chevrolet 22.1 14.9
Dodge 17.6 13.0
Ford 20.5 14.1
Honda 33.8 24.0
Hyundai 27.0 18.9
Jeep 17.3 13.2
Land rover 18.0 12.0
Lincoln 18.0 12.0
Mercury 19.0 13.0
Nissan 25.6 18.4
Pontiac 26.5 17.0
Subaru 25.6 19.5
Toyota 26.1 19.1
Volkswagen 28.5 20.5
Note:
Source: mpg data set.
  1. Conduct a linear regression called my_model of the effects of displacement, cylinder, and year on highway fuel efficiency: lm(hwy ~ displ + cyl + year, my_mpg). Apply the summary() function to the model object, then return the coefficients table from the summary.
my_model <- lm(hwy ~ displ + cyl + year, my_mpg)
my_model_summary <- summary(my_model)$coefficients
kable(my_model_summary)
Estimate Std. Error t value Pr(>|t|)
(Intercept) -259.1221270 109.1594125 -2.373795 0.0184288
displ -2.0912178 0.5146232 -4.063590 0.0000663
cyl -1.3065333 0.4111205 -3.177982 0.0016866
year 0.1484984 0.0545146 2.724013 0.0069446
  1. Extract the table after applying {papaja}’s apa_print() to my_model, then pass this to apa_table().
library(papaja)
apa_table(apa_print(my_model)$table)
(#tab:unnamed-chunk-6)
Predictor \(b\) 95% CI \(t\) \(\mathit{df}\) \(p\)
Intercept -259.12 [-474.20, -44.04] -2.37 230 .018
Displ -2.09 [-3.11, -1.08] -4.06 230 < .001
Cyl -1.31 [-2.12, -0.50] -3.18 230 .002
Year 0.15 [0.04, 0.26] 2.72 230 .007