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)
library(dplyr)
flights %>% group_by(month) %>%
summarize(cancel_count =sum(is.na(dep_time)),
cancel_proportion=cancel_count/n())%>%arrange(desc(cancel_proportion))
## # A tibble: 12 × 3
##    month cancel_count cancel_proportion
##    <int>        <int>             <dbl>
##  1     2         1261           0.0505 
##  2    12         1025           0.0364 
##  3     6         1009           0.0357 
##  4     7          940           0.0319 
##  5     3          861           0.0299 
##  6     4          668           0.0236 
##  7     5          563           0.0196 
##  8     1          521           0.0193 
##  9     8          486           0.0166 
## 10     9          452           0.0164 
## 11    11          233           0.00854
## 12    10          236           0.00817

Hence, most flights got cancelled in February and September had lowest count of cancelled flights.

Question #2

Consider the following pipeline:

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

What is the problem with this pipeline?

The am column needs to be added to the group by() verb

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)

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

head(Teams$BA)
## [1] 0.3104956 0.2700669 0.2765599 0.2386059 0.2870370 0.3200625
head(Teams$SLG)
## [1] 0.5021866 0.4431438 0.4603710 0.3324397 0.3960114 0.5144418

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)%>%
  summarize(homeRuns =sum(HR),stolen_bases=sum(SB))%>%
  filter(homeRuns>=300&stolen_bases>=300)%>%
  inner_join(People,by =c("playerID"="playerID"))%>%
  select(nameFirst, nameLast, nameGiven, homeRuns, stolen_bases)
## # A tibble: 8 × 5
##   nameFirst nameLast  nameGiven          homeRuns stolen_bases
##   <chr>     <chr>     <chr>                 <int>        <int>
## 1 Carlos    Beltran   Carlos Ivan             435          312
## 2 Barry     Bonds     Barry Lamar             762          514
## 3 Bobby     Bonds     Bobby Lee               332          461
## 4 Andre     Dawson    Andre Nolan             438          314
## 5 Steve     Finley    Steven Allen            304          320
## 6 Willie    Mays      Willie Howard           660          338
## 7 Alex      Rodriguez Alexander Enmanuel      696          329
## 8 Reggie    Sanders   Reginald Laverne        305          304
Pitching%>%
  group_by(playerID)%>%
  summarize(wins =sum(W),strikeouts=sum(SO))%>%
  filter(wins>=300&strikeouts>=3000)%>%
  inner_join(People,by =c('playerID'='playerID'))%>%
  select(nameFirst, nameLast, nameGiven, wins, strikeouts)
## # A tibble: 10 × 5
##    nameFirst nameLast nameGiven        wins strikeouts
##    <chr>     <chr>    <chr>           <int>      <int>
##  1 Steve     Carlton  Steven Norman     329       4136
##  2 Roger     Clemens  William Roger     354       4672
##  3 Randy     Johnson  Randall David     303       4875
##  4 Walter    Johnson  Walter Perry      417       3509
##  5 Greg      Maddux   Gregory Alan      355       3371
##  6 Phil      Niekro   Philip Henry      318       3342
##  7 Gaylord   Perry    Gaylord Jackson   314       3534
##  8 Nolan     Ryan     Lynn Nolan        324       5714
##  9 Tom       Seaver   George Thomas     311       3640
## 10 Don       Sutton   Donald Howard     324       3574
Batting%>%
  group_by(playerID, yearID)%>%
  summarize(homeRuns =sum(HR),battingAvg=sum(H)/sum(AB))%>%
  filter(homeRuns>=50)%>%
  inner_join(People,by =c("playerID"="playerID"))%>%
  select(yearID, nameFirst, nameLast, nameGiven, homeRuns,battingAvg)%>%
  arrange(battingAvg)
## # A tibble: 46 × 7
## # Groups:   playerID [30]
##    playerID  yearID nameFirst nameLast nameGiven              homeRuns batting…¹
##    <chr>      <int> <chr>     <chr>    <chr>                     <int>     <dbl>
##  1 alonspe01   2019 Pete      Alonso   Peter Morgan                 53     0.260
##  2 bautijo02   2010 Jose      Bautista Jose Antonio                 54     0.260
##  3 jonesan01   2005 Andruw    Jones    Andruw Rudolf                51     0.263
##  4 marisro01   1961 Roger     Maris    Roger Eugene                 61     0.269
##  5 vaughgr01   1998 Greg      Vaughn   Gregory Lamont               50     0.272
##  6 mcgwima01   1997 Mark      McGwire  Mark David                   58     0.274
##  7 fieldce01   1990 Cecil     Fielder  Cecil Grant                  51     0.277
##  8 mcgwima01   1999 Mark      McGwire  Mark David                   65     0.278
##  9 stantmi03   2017 Giancarlo Stanton  Giancarlo Cruz-Michael       59     0.281
## 10 judgeaa01   2017 Aaron     Judge    Aaron James                  52     0.284
## # … with 36 more rows, and abbreviated variable name ¹​battingAvg