1. Self-Reported Heights

Density plots and empirical cumulative distribution plots for the two groups:

library(patchwork)
data(heights, package = "dslabs")
thm <- theme_minimal() + theme(text = element_text(size = 16))
p1 <- ggplot(heights, aes(x = height, fill = sex)) +
    geom_density(alpha = 0.6) +
    thm + theme(legend.position = "top")
p2 <- ggplot(heights, aes(x = height, color = sex)) +
    stat_ecdf() +
    thm + theme(legend.position = "top")
p1 | p2

The density plots make it easier to identify general characteristics of the distribution: close to symmetric, not too far from bell-shaped.

Reading off percentiles, such as medians of quartiles, is easier in the eCDF plots.

2. Top Fleet Highway Gas Mileages

The following table shows the manufacturers with top five fleet average highway gas mileage values and the corresponging average city gas mileages:

library(readr)
if (! file.exists("vehicles.csv.zip"))
    download.file("http://www.stat.uiowa.edu/~luke/data/vehicles.csv.zip",
                  "vehicles.csv.zip")
newmpg <- read_csv("vehicles.csv.zip", guess_max = 100000)

tbl <- filter(newmpg, year == 2021) %>%
    group_by(make) %>%
    summarize(avg_hwy = mean(highway08),
              avg_cty = mean(city08)) %>%
    ungroup() %>%
    top_n(5, avg_hwy) %>%
    arrange(desc(avg_hwy))
names(tbl) <- c("Make", "Highway", "City")
kbl <- knitr::kable(tbl, format = "html", digits = 1)
kableExtra::kable_styling(kbl, full_width = FALSE)
Make Highway City
Tesla 107.8 117.8
Kandi 102.0 127.0
Polestar 59.5 59.0
Volkswagen 41.3 38.0
Hyundai 39.5 35.5

3. Top Destinations

The top four destinations are shown in the following table:

top_nyc_dest <- count(flights, dest) %>% top_n(4, n)
kbl <- knitr::kable(top_nyc_dest, format = "html")
kableExtra::kable_styling(kbl, full_width = FALSE)
dest n
ATL 17215
BOS 15508
LAX 16174
ORD 17283

The flights to these locations can be selected using semi_join. After using count to compute the number of flights to each origin/destination the counts for each destination can be converted to proportion with a grouped mutate. The resulting faceted bar chart:

top_nyc_dest <- count(flights, dest) %>% top_n(4, n)

semi_join(flights, top_nyc_dest, "dest") %>%
    count(origin, dest) %>%
    group_by(dest) %>%
    mutate(prop = n / sum(n)) %>%
    ungroup() %>%
    ggplot() +
    geom_col(aes(x = origin, y = prop), fill = "deepskyblue3") +
    facet_wrap(~ dest, ncol = 1) +
    thm +
    labs(x = "Origin", y = "Proportion")

There are no flights from LGA to LAX and few from JFK to either ATL or ORD.

4. Summer-Only Destinations

fs <- filter(flights, month %in% 6 : 8) %>% select(dest) %>% unique()
fn <- filter(flights, ! (month %in% 6 : 8)) %>% select(dest) %>% unique()
summer_only <- anti_join(fs, fn, "dest")
summer_dest <- semi_join(airports, summer_only, c(faa = "dest")) %>%
    select(faa, name)

There were 3 destinations with flights only in June through August:

knitr::kable(summer_dest, format = "html") %>%
    kableExtra::kable_styling(full_width = FALSE)
faa name
ANC Ted Stevens Anchorage Intl
LGA La Guardia
TVC Cherry Capital Airport

Apparently there was a flight on US Airways from EWR to LGA on July 27.

5. High Altitude Destinations

The destinations at altitudes above 6,000 feet and the number of flights in 2013 from New York City to these destinations were:

airports1 <- select(airports, faa, name, alt)
left_join(flights, airports1, c("dest" = "faa")) %>%
    filter(alt > 6000) %>%
    count(dest, name, alt) %>%
    select(FAA = dest, Name = name, Altitude = alt, Flights = n) %>%
    knitr::kable(format = "html") %>%
    kableExtra::kable_styling(full_width = FALSE)
FAA Name Altitude Flights
EGE Eagle Co Rgnl 6540 213
HDN Yampa Valley 6602 15
JAC Jackson Hole Airport 6451 25
LS0tCnRpdGxlOiAiQXNzaWdubWVudCA2IE5vdGVzIgpvdXRwdXQ6CiAgaHRtbF9kb2N1bWVudDoKICAgIHRvYzogeWVzCiAgICBjb2RlX2Rvd25sb2FkOiB0cnVlCiAgICBjb2RlX2ZvbGRpbmc6ICJoaWRlIgotLS0KCSAgCmBgYHtyIGdsb2JhbF9vcHRpb25zLCBpbmNsdWRlID0gRkFMU0V9CmtuaXRyOjpvcHRzX2NodW5rJHNldChjb2xsYXBzZSA9IFRSVUUpCmBgYAoKYGBge3IsIGluY2x1ZGUgPSBGQUxTRX0KbGlicmFyeShkcGx5cikKbGlicmFyeShueW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