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?
February
b.What month had the lowest?
October
library(nycflights13)
flightCancellationPropByMonth <-
flights %>%
group_by(month) %>%
summarise(
cancelled = sum(is.na(dep_time)),
cancelledProportion = (cancelled/n())*100,
N = n()
) %>%
arrange(cancelledProportion)
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 data should be filtered first and then summarized. If the
data is summarized first, the columns/variables may not be present on
which filter() is applied like we see in the above example. To fix this,
apply filter( ) first followed by summarize( ).
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
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,
SLG = (H+2*X2B+3*X3B+4*HR)/AB
)
head(select(Teams, BA, SLG))
## BA SLG
## 1 0.3104956 0.5021866
## 2 0.2700669 0.4431438
## 3 0.2765599 0.4603710
## 4 0.2386059 0.3324397
## 5 0.2870370 0.3960114
## 6 0.3200625 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.
Top 5 teams in terms of SLG every year since 1969:
library(Lahman)
Top5SLGbyYear <-
filter(Teams, yearID >= "1969") %>%
group_by(yearID) %>%
arrange(desc(SLG)) %>%
slice(1:5) %>%
select(yearID, name, SLG)
Top5SLGbyYear
## # A tibble: 265 × 3
## # Groups: yearID [53]
## yearID name SLG
## <int> <chr> <dbl>
## 1 1969 Boston Red Sox 0.500
## 2 1969 Cincinnati Reds 0.500
## 3 1969 Baltimore Orioles 0.493
## 4 1969 Minnesota Twins 0.486
## 5 1969 Pittsburgh Pirates 0.467
## 6 1970 Cincinnati Reds 0.524
## 7 1970 Boston Red Sox 0.515
## 8 1970 Chicago Cubs 0.497
## 9 1970 San Francisco Giants 0.491
## 10 1970 Pittsburgh Pirates 0.483
## # … with 255 more rows
Top 5 teams in terms of SLG (all time) since 1969:
Top5SLGsince1969 <-
filter(Teams, yearID >= "1969") %>%
arrange(desc(SLG)) %>%
slice(1:5) %>%
select(yearID, name, SLG)
Top5SLGsince1969
## yearID name SLG
## 1 2019 Houston Astros 0.6092998
## 2 2019 Minnesota Twins 0.6071179
## 3 2003 Boston Red Sox 0.6033975
## 4 2019 New York Yankees 0.5996776
## 5 2020 Atlanta Braves 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.
library(Lahman)
Batting %>%
group_by(playerID) %>%
summarise(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
Pitching %>%
group_by(playerID) %>%
summarise(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
Batting %>%
group_by(playerID, yearID) %>%
summarize(homeRuns = sum(HR), battingAverage = sum(H)/sum(AB)) %>%
filter(homeRuns >= 50) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(yearID, playerID, nameFirst, nameLast, nameGiven, homeRuns, battingAverage) %>%
arrange(battingAverage)
## # A tibble: 46 × 7
## # Groups: playerID [30]
## yearID playerID nameFirst nameLast nameGiven homeRuns battingAverage
## <int> <chr> <chr> <chr> <chr> <int> <dbl>
## 1 2019 alonspe01 Pete Alonso Peter Morgan 53 0.260
## 2 2010 bautijo02 Jose Bautista Jose Antonio 54 0.260
## 3 2005 jonesan01 Andruw Jones Andruw Rudolf 51 0.263
## 4 1961 marisro01 Roger Maris Roger Eugene 61 0.269
## 5 1998 vaughgr01 Greg Vaughn Gregory Lamont 50 0.272
## 6 1997 mcgwima01 Mark McGwire Mark David 58 0.274
## 7 1990 fieldce01 Cecil Fielder Cecil Grant 51 0.277
## 8 1999 mcgwima01 Mark McGwire Mark David 65 0.278
## 9 2017 stantmi03 Giancarlo Stanton Giancarlo Cruz-M… 59 0.281
## 10 2017 judgeaa01 Aaron Judge Aaron James 52 0.284
## # … with 36 more rows