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? b.What month had the lowest?
library(nycflights13)
flights %>%
group_by(month) %>%
summarise(cancelled = sum(is.na(air_time)),
total = n(),
prop = cancelled/total * 100) %>%
arrange(desc(prop))
## # A tibble: 12 x 4
## month cancelled total prop
## <int> <int> <int> <dbl>
## 1 2 1340 24951 5.37
## 2 6 1168 28243 4.14
## 3 12 1115 28135 3.96
## 4 7 1132 29425 3.85
## 5 3 932 28834 3.23
## 6 4 766 28330 2.70
## 7 5 668 28796 2.32
## 8 1 606 27004 2.24
## 9 9 564 27574 2.05
## 10 8 571 29327 1.95
## 11 11 297 27268 1.09
## 12 10 271 28889 0.938
#a.month 2
#b.month 10
Question #2
Consider the following pipeline:
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
filter(am == 1) %>%
summarize(avg_mpg = mean(mpg))
## # A tibble: 3 x 2
## cyl avg_mpg
## <dbl> <dbl>
## 1 4 28.1
## 2 6 20.6
## 3 8 15.4
#filter should come before summarize function
What is the problem with this pipeline?
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)
df<-
Teams %>%
mutate(BA = H/AB*100,
SLG = (1 * 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)
df %>%
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.
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) %>%
summarise(total_HR = sum(HR), total_SB = sum(SB)) %>%
filter(total_HR>=300 & total_SB >= 300) %>%
left_join(People, by = 'playerID') %>%
select(nameFirst, nameLast, total_HR, total_SB)
## # A tibble: 8 x 4
## nameFirst nameLast total_HR total_SB
## <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) %>%
summarise(total_W = sum(W), total_SO = sum(SO)) %>%
filter(total_W>=300 & total_SO >= 3000) %>%
left_join(People, by = 'playerID') %>%
select(nameFirst, nameLast, total_W, total_SO)
## # A tibble: 10 x 4
## nameFirst nameLast total_W total_SO
## <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) %>%
summarise(total_HR = sum(HR),BA=sum(H)/sum(AB)) %>%
filter(total_HR>=50) %>%
left_join(People, by = 'playerID') %>%
select(nameFirst, nameLast, total_HR,BA, yearID) %>%
arrange(BA)
## # A tibble: 46 x 6
## # Groups: playerID [30]
## playerID nameFirst nameLast total_HR BA yearID
## <chr> <chr> <chr> <int> <dbl> <int>
## 1 alonspe01 Pete Alonso 53 0.260 2019
## 2 bautijo02 Jose Bautista 54 0.260 2010
## 3 jonesan01 Andruw Jones 51 0.263 2005
## 4 marisro01 Roger Maris 61 0.269 1961
## 5 vaughgr01 Greg Vaughn 50 0.272 1998
## 6 mcgwima01 Mark McGwire 58 0.274 1997
## 7 fieldce01 Cecil Fielder 51 0.277 1990
## 8 mcgwima01 Mark McGwire 65 0.278 1999
## 9 stantmi03 Giancarlo Stanton 59 0.281 2017
## 10 judgeaa01 Aaron Judge 52 0.284 2017
## # ... with 36 more rows