Directions

During ANLY 512 we will be studying the theory and practice of data visualization. We will be using R and the packages within R to assemble data and construct many different types of visualizations. Before we begin studying data visualizations we need to develop some data wrangling skills. We will use these skills to wrangle our data into a form that we can use for visualizations.

The objective of this assignment is to introduce you to R Studio, Rmarkdown, the tidyverse and more specifically the dplyr package.

Each question is worth 5 points.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Canvas. Please make sure that this link is hyper linked and that I can see the visualization and the code required to create it.

Question #1

Use the nycflights13 package and the flights data frame to answer the following questions: a.What month had the highest proportion of cancelled flights? b.What month had the lowest?

library(nycflights13)
flight_cancel <- flights %>%
  group_by(month) %>%
  summarize(cancelled = sum(is.na(dep_time)), 
            cancelled_proportion = cancelled/n()*100) %>%
  arrange(cancelled_proportion)

flight_cancel
## # A tibble: 12 × 3
##    month cancelled cancelled_proportion
##    <int>     <int>                <dbl>
##  1    10       236                0.817
##  2    11       233                0.854
##  3     9       452                1.64 
##  4     8       486                1.66 
##  5     1       521                1.93 
##  6     5       563                1.96 
##  7     4       668                2.36 
##  8     3       861                2.99 
##  9     7       940                3.19 
## 10     6      1009                3.57 
## 11    12      1025                3.64 
## 12     2      1261                5.05
# a. October had the highest proportion of cancelled flights
# b. June had the lowest proportion of cancelled flights

Question #2

Consider the following pipeline:

library(tidyverse)
mtcars %>%
  group_by(cyl) %>%
  summarize(avg_mpg = mean(mpg)) %>%
  filter(am == 1)

#variable 'am' does not exist after the summarize function. So we can't filter 'am' after summarize. We should call filter function before summarize.

library(tidyverse)
mtcars %>%
  group_by(cyl) %>%
  filter(am == 1) %>%
  summarize(avg_mpg = mean(mpg))

What is the problem with this pipeline?

Question #3

Define two new variables in the Teams data frame in the pkg Lahman() package.

  1. batting average (BA). Batting average is the ratio of hits (H) to at-bats (AB)

  2. slugging percentage (SLG). Slugging percentage is total bases divided by at-bats (AB). To compute total bases, you get 1 for a single, 2 for a double, 3 for a triple, and 4 for a home run.

library(Lahman)

Teams <- Teams %>%
    mutate(BA = H/AB) %>%
    mutate(SLG = (H+2*X2B+3*X3B+4*HR)/AB)

Question #4

Using the Teams data frame in the pkg Lahman() package. display the top-5 teams ranked in terms of slugging percentage (SLG) in Major League Baseball history. Repeat this using teams since 1969. Slugging percentage is total bases divided by at-bats.To compute total bases, you get 1 for a single, 2 for a double, 3 for a triple, and 4 for a home run.

library(Lahman)

Teams %>%
  select(yearID, teamID, SLG) %>%
  arrange(desc(SLG)) %>%
  head(5)
##   yearID teamID       SLG
## 1   2019    HOU 0.6092998
## 2   2019    MIN 0.6071179
## 3   2003    BOS 0.6033975
## 4   2019    NYA 0.5996776
## 5   2020    ATL 0.5964320
Teams %>%
  select(yearID,teamID, SLG) %>%
  filter(yearID >= 1969) %>%
  arrange(desc(SLG)) %>%
  head(5)
##   yearID teamID       SLG
## 1   2019    HOU 0.6092998
## 2   2019    MIN 0.6071179
## 3   2003    BOS 0.6033975
## 4   2019    NYA 0.5996776
## 5   2020    ATL 0.5964320

Question #5

Use the Batting, Pitching, and People tables in the pkg Lahman() package to answer the following questions.

a.Name every player in baseball history who has accumulated at least 300 home runs (HR) and at least 300 stolen bases (SB). You can find the first and last name of the player in the Master data frame. Join this to your result along with the total home runs and total bases stolen for each of these elite players.

  1. Similarly, name every pitcher in baseball history who has accumulated at least 300 wins (W) and at least 3,000 strikeouts (SO).

  2. Identify the name and year of every player who has hit at least 50 home runs in a single season. Which player had the lowest batting average in that season?

library(Lahman)

Batting %>%
  group_by(playerID) %>%
  summarise(home_runs = sum(HR), stolen_bases = sum(SB)) %>%
  filter(home_runs >= 300 & stolen_bases >= 300) %>%
  left_join(People, by = c("playerID" = "playerID")) %>%
  select(nameFirst, nameLast, home_runs, stolen_bases)
## # A tibble: 8 × 4
##   nameFirst nameLast  home_runs stolen_bases
##   <chr>     <chr>         <int>        <int>
## 1 Carlos    Beltran         435          312
## 2 Barry     Bonds           762          514
## 3 Bobby     Bonds           332          461
## 4 Andre     Dawson          438          314
## 5 Steve     Finley          304          320
## 6 Willie    Mays            660          338
## 7 Alex      Rodriguez       696          329
## 8 Reggie    Sanders         305          304
Pitching %>%
  group_by(playerID) %>%
  summarise(wins = sum(W), strikeouts = sum(SO)) %>%
  filter(wins >= 300 & strikeouts >= 3000) %>%
  left_join(People, by = c("playerID" = "playerID")) %>%
  select(nameFirst, nameLast, wins, strikeouts)
## # A tibble: 10 × 4
##    nameFirst nameLast  wins strikeouts
##    <chr>     <chr>    <int>      <int>
##  1 Steve     Carlton    329       4136
##  2 Roger     Clemens    354       4672
##  3 Randy     Johnson    303       4875
##  4 Walter    Johnson    417       3509
##  5 Greg      Maddux     355       3371
##  6 Phil      Niekro     318       3342
##  7 Gaylord   Perry      314       3534
##  8 Nolan     Ryan       324       5714
##  9 Tom       Seaver     311       3640
## 10 Don       Sutton     324       3574
Batting %>%
  group_by(playerID, yearID) %>%
  summarise(home_runs = sum(HR), BA = sum(H)/sum(AB)) %>%
  filter(home_runs >= 50) %>%
  left_join(People, by = c("playerID" = "playerID")) %>%
  select(yearID,nameFirst, nameLast, home_runs, BA) %>%
  arrange(desc(BA))
## # A tibble: 46 × 6
## # Groups:   playerID [30]
##    playerID  yearID nameFirst nameLast home_runs    BA
##    <chr>      <int> <chr>     <chr>        <int> <dbl>
##  1 ruthba01    1921 Babe      Ruth            59 0.378
##  2 ruthba01    1920 Babe      Ruth            54 0.376
##  3 foxxji01    1932 Jimmie    Foxx            58 0.364
##  4 ruthba01    1927 Babe      Ruth            60 0.356
##  5 wilsoha01   1930 Hack      Wilson          56 0.356
##  6 mantlmi01   1956 Mickey    Mantle          52 0.353
##  7 foxxji01    1938 Jimmie    Foxx            50 0.349
##  8 bondsba01   2001 Barry     Bonds           73 0.328
##  9 sosasa01    2001 Sammy     Sosa            64 0.328
## 10 gonzalu01   2001 Luis      Gonzalez        57 0.325
## # … with 36 more rows