dplyr
basicsdplyrDuring 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? Month 2 (Feb) b.What month had the lowest? Month 10 (Oct)
library(nycflights13)
flights[is.na(flights$dep_time),]
## # A tibble: 8,255 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 NA 1630 NA NA 1815
## 2 2013 1 1 NA 1935 NA NA 2240
## 3 2013 1 1 NA 1500 NA NA 1825
## 4 2013 1 1 NA 600 NA NA 901
## 5 2013 1 2 NA 1540 NA NA 1747
## 6 2013 1 2 NA 1620 NA NA 1746
## 7 2013 1 2 NA 1355 NA NA 1459
## 8 2013 1 2 NA 1420 NA NA 1644
## 9 2013 1 2 NA 1321 NA NA 1536
## 10 2013 1 2 NA 1545 NA NA 1910
## # … with 8,245 more rows, and 11 more variables: arr_delay <dbl>,
## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>
unique(flights$year)
## [1] 2013
flight_cancellations <- flights %>%
group_by(month) %>%
summarize(cancelled = sum(is.na(dep_time)),
cancelled_proportion = cancelled/n()*100) %>%
arrange(cancelled_proportion)
flight_cancellations
## # 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
Question #2
Consider the following pipeline:
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg)) %>%
filter(am == 1)
#edited code:
mtcars %>%
filter(am == 1) %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg))
What is the problem with this pipeline? You cannot filter on am since it is not in the results after aggregating mtcars. As an alternative (edited code), the first step could be to filter on am and then avg mpg can be calculated for each cyl.
Question #3
Define two new variables in the Teams data frame in the
pkg Lahman() package.
batting average (BA). Batting average is the ratio of hits (H) to at-bats (AB)
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)
summary(Teams)
## yearID lgID teamID franchID divID
## Min. :1871 AA: 85 CHN : 146 ATL : 146 Length:2985
## 1st Qu.:1922 AL:1295 PHI : 139 CHC : 146 Class :character
## Median :1967 FL: 16 PIT : 135 CIN : 140 Mode :character
## Mean :1959 NA: 50 CIN : 132 PIT : 140
## 3rd Qu.:1997 NL:1519 SLN : 130 STL : 140
## Max. :2021 PL: 8 BOS : 121 PHI : 139
## UA: 12 (Other):2182 (Other):2134
## Rank G Ghome W
## Min. : 1.000 Min. : 6 Min. :24.00 Min. : 0.00
## 1st Qu.: 2.000 1st Qu.:154 1st Qu.:77.00 1st Qu.: 66.00
## Median : 4.000 Median :159 Median :81.00 Median : 77.00
## Mean : 4.039 Mean :150 Mean :78.05 Mean : 74.61
## 3rd Qu.: 6.000 3rd Qu.:162 3rd Qu.:81.00 3rd Qu.: 87.00
## Max. :13.000 Max. :165 Max. :84.00 Max. :116.00
## NA's :399
## L DivWin WCWin LgWin
## Min. : 4.00 Length:2985 Length:2985 Length:2985
## 1st Qu.: 65.00 Class :character Class :character Class :character
## Median : 76.00 Mode :character Mode :character Mode :character
## Mean : 74.61
## 3rd Qu.: 87.00
## Max. :134.00
##
## WSWin R AB H
## Length:2985 Min. : 24 Min. : 211 Min. : 33
## Class :character 1st Qu.: 614 1st Qu.:5135 1st Qu.:1299
## Mode :character Median : 691 Median :5402 Median :1390
## Mean : 681 Mean :5129 Mean :1339
## 3rd Qu.: 764 3rd Qu.:5519 3rd Qu.:1465
## Max. :1220 Max. :5781 Max. :1783
##
## X2B X3B HR BB
## Min. : 1.0 Min. : 0.00 Min. : 0.0 Min. : 1.0
## 1st Qu.:194.0 1st Qu.: 29.00 1st Qu.: 45.0 1st Qu.:425.8
## Median :234.0 Median : 40.00 Median :110.0 Median :494.0
## Mean :228.7 Mean : 45.67 Mean :105.9 Mean :473.6
## 3rd Qu.:272.0 3rd Qu.: 59.00 3rd Qu.:155.0 3rd Qu.:554.2
## Max. :376.0 Max. :150.00 Max. :307.0 Max. :835.0
## NA's :1
## SO SB CS HBP
## Min. : 3.0 Min. : 1.0 Min. : 3.00 Min. : 7.00
## 1st Qu.: 516.0 1st Qu.: 62.5 1st Qu.: 33.00 1st Qu.: 32.00
## Median : 761.0 Median : 93.0 Median : 44.00 Median : 43.00
## Mean : 762.1 Mean :109.4 Mean : 46.55 Mean : 45.82
## 3rd Qu.: 990.0 3rd Qu.:137.0 3rd Qu.: 56.00 3rd Qu.: 57.00
## Max. :1596.0 Max. :581.0 Max. :191.00 Max. :160.00
## NA's :16 NA's :126 NA's :832 NA's :1158
## SF RA ER ERA
## Min. : 7.00 Min. : 34 Min. : 23.0 Min. :1.220
## 1st Qu.:38.00 1st Qu.: 610 1st Qu.: 503.0 1st Qu.:3.370
## Median :44.00 Median : 689 Median : 594.0 Median :3.840
## Mean :44.11 Mean : 681 Mean : 573.4 Mean :3.841
## 3rd Qu.:50.00 3rd Qu.: 766 3rd Qu.: 671.0 3rd Qu.:4.330
## Max. :77.00 Max. :1252 Max. :1023.0 Max. :8.000
## NA's :1541
## CG SHO SV IPouts
## Min. : 0.00 Min. : 0.000 Min. : 0.00 Min. : 162
## 1st Qu.: 9.00 1st Qu.: 6.000 1st Qu.:10.00 1st Qu.:4080
## Median : 41.00 Median : 9.000 Median :25.00 Median :4252
## Mean : 47.55 Mean : 9.588 Mean :24.42 Mean :4013
## 3rd Qu.: 76.00 3rd Qu.:12.000 3rd Qu.:39.00 3rd Qu.:4341
## Max. :148.00 Max. :32.000 Max. :68.00 Max. :4518
##
## HA HRA BBA SOA
## Min. : 49 Min. : 0.0 Min. : 1.0 Min. : 0.0
## 1st Qu.:1287 1st Qu.: 51.0 1st Qu.:429.0 1st Qu.: 511.0
## Median :1389 Median :113.0 Median :495.0 Median : 762.0
## Mean :1339 Mean :105.9 Mean :473.7 Mean : 761.6
## 3rd Qu.:1468 3rd Qu.:153.0 3rd Qu.:554.0 3rd Qu.: 997.0
## Max. :1993 Max. :305.0 Max. :827.0 Max. :1687.0
##
## E DP FP name
## Min. : 20.0 Min. : 0.0 Min. :0.7610 Length:2985
## 1st Qu.:111.0 1st Qu.:116.0 1st Qu.:0.9660 Class :character
## Median :141.0 Median :140.0 Median :0.9770 Mode :character
## Mean :180.8 Mean :132.6 Mean :0.9664
## 3rd Qu.:207.0 3rd Qu.:157.0 3rd Qu.:0.9810
## Max. :639.0 Max. :217.0 Max. :0.9910
##
## park attendance BPF PPF
## Length:2985 Min. : 0 Min. : 60.0 Min. : 60.0
## Class :character 1st Qu.: 538461 1st Qu.: 97.0 1st Qu.: 97.0
## Mode :character Median :1190886 Median :100.0 Median :100.0
## Mean :1376599 Mean :100.2 Mean :100.2
## 3rd Qu.:2066598 3rd Qu.:103.0 3rd Qu.:103.0
## Max. :4483350 Max. :129.0 Max. :141.0
## NA's :279
## teamIDBR teamIDlahman45 teamIDretro BA
## Length:2985 Length:2985 Length:2985 Min. :0.1564
## Class :character Class :character Class :character 1st Qu.:0.2494
## Mode :character Mode :character Mode :character Median :0.2600
## Mean :0.2607
## 3rd Qu.:0.2708
## Max. :0.3498
##
## SLG
## Min. :0.1659
## 1st Qu.:0.4192
## Median :0.4596
## Mean :0.4561
## 3rd Qu.:0.4950
## Max. :0.6093
##
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 since 1969.
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.
Similarly, name every pitcher in baseball history who has accumulated at least 300 wins (W) and at least 3,000 strikeouts (SO).
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)
#a)
Batting %>%
group_by(playerID) %>%
summarize(totalHR = sum(HR), totalSB = sum(SB)) %>%
filter(totalHR >= 300 & totalSB >= 300) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, totalHR, totalSB)
## # A tibble: 8 × 4
## nameFirst nameLast totalHR totalSB
## <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
#b)
Pitching %>%
group_by(playerID) %>%
summarize(totalW = sum(W), totalSO = sum(SO)) %>%
filter(totalW >= 300 & totalSO >= 3000) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, totalW, totalSO)
## # A tibble: 10 × 4
## nameFirst nameLast totalW totalSO
## <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
#c)
Batting %>%
group_by(playerID, yearID) %>%
summarize(totalHR = sum(HR), BA = sum(H)/sum(AB)) %>%
filter(totalHR >= 50) %>%
inner_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, yearID, totalHR, BA) %>%
ungroup() %>%
arrange(BA)
## # A tibble: 46 × 6
## playerID nameFirst nameLast yearID totalHR BA
## <chr> <chr> <chr> <int> <int> <dbl>
## 1 alonspe01 Pete Alonso 2019 53 0.260
## 2 bautijo02 Jose Bautista 2010 54 0.260
## 3 jonesan01 Andruw Jones 2005 51 0.263
## 4 marisro01 Roger Maris 1961 61 0.269
## 5 vaughgr01 Greg Vaughn 1998 50 0.272
## 6 mcgwima01 Mark McGwire 1997 58 0.274
## 7 fieldce01 Cecil Fielder 1990 51 0.277
## 8 mcgwima01 Mark McGwire 1999 65 0.278
## 9 stantmi03 Giancarlo Stanton 2017 59 0.281
## 10 judgeaa01 Aaron Judge 2017 52 0.284
## # … with 36 more rows