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? – Feb b.What month had the lowest? – Oct
library(nycflights13)
flights_highest <- flights %>%
group_by(month) %>%
summarize(cancelled = sum(is.na(arr_delay)),
total = n(),
prop_cancelled = cancelled/total) %>%
arrange(desc(prop_cancelled))
flights_highest
## # A tibble: 12 × 4
## month cancelled total prop_cancelled
## <int> <int> <int> <dbl>
## 1 2 1340 24951 0.0537
## 2 6 1168 28243 0.0414
## 3 12 1115 28135 0.0396
## 4 7 1132 29425 0.0385
## 5 3 932 28834 0.0323
## 6 4 766 28330 0.0270
## 7 5 668 28796 0.0232
## 8 1 606 27004 0.0224
## 9 9 564 27574 0.0205
## 10 8 571 29327 0.0195
## 11 11 297 27268 0.0109
## 12 10 271 28889 0.00938
flights_lowest <- flights %>%
group_by(month) %>%
summarize(cancelled = sum(is.na(arr_delay)),
total = n(),
prop_cancelled = cancelled/total) %>%
arrange((prop_cancelled))
flights_lowest
## # A tibble: 12 × 4
## month cancelled total prop_cancelled
## <int> <int> <int> <dbl>
## 1 10 271 28889 0.00938
## 2 11 297 27268 0.0109
## 3 8 571 29327 0.0195
## 4 9 564 27574 0.0205
## 5 1 606 27004 0.0224
## 6 5 668 28796 0.0232
## 7 4 766 28330 0.0270
## 8 3 932 28834 0.0323
## 9 7 1132 29425 0.0385
## 10 12 1115 28135 0.0396
## 11 6 1168 28243 0.0414
## 12 2 1340 24951 0.0537
Question #2
Consider the following pipeline:
library(tidyverse)
mtcars %>%
filter(am == 1) %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg))
## # A tibble: 3 × 2
## cyl avg_mpg
## <dbl> <dbl>
## 1 4 28.1
## 2 6 20.6
## 3 8 15.4
What is the problem with this pipeline?
- filter command should be placed first.
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
## Repeat this using 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 Master 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?
# part a
library(Lahman)
Batting %>%
group_by(playerID) %>%
summarize(home_runs = sum(HR), stolen_bases = sum (SB)) %>%
filter(home_runs >= 300 & stolen_bases >= 300) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, nameGiven, home_runs, stolen_bases)
## # A tibble: 8 × 5
## nameFirst nameLast nameGiven home_runs 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
# part b:
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
# part c:
Batting %>%
group_by(playerID, yearID) %>%
summarize(home_runs = sum(HR), batting_avg = sum(H)/sum(AB)) %>%
filter(home_runs >= 50) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(yearID, nameFirst, nameLast, nameGiven, home_runs, batting_avg) %>%
arrange(batting_avg)
## # A tibble: 46 × 7
## # Groups: playerID [30]
## playerID yearID nameFirst nameLast nameGiven home_runs batting_avg
## <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-Mic… 59 0.281
## 10 judgeaa01 2017 Aaron Judge Aaron James 52 0.284
## # … with 36 more rows