dplyr
basicsdplyr
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)
#assuming flights with dep_time "NA" on it were cancelled since there were no specific column for cancelled flights
flights$month <- as.factor(flights$month)
cancel_proportion <- flights %>%
group_by(month) %>%
summarize(cancelled = sum(is.na(dep_time)),
total = n(),
proportion_cancelled = cancelled/total) %>%
arrange(desc(proportion_cancelled))
cancel_proportion
## # A tibble: 12 × 4
## month cancelled total proportion_cancelled
## <fct> <int> <int> <dbl>
## 1 2 1261 24951 0.0505
## 2 12 1025 28135 0.0364
## 3 6 1009 28243 0.0357
## 4 7 940 29425 0.0319
## 5 3 861 28834 0.0299
## 6 4 668 28330 0.0236
## 7 5 563 28796 0.0196
## 8 1 521 27004 0.0193
## 9 8 486 29327 0.0166
## 10 9 452 27574 0.0164
## 11 11 233 27268 0.00854
## 12 10 236 28889 0.00817
cancel_proportion %>% ggplot(aes(x = month, y = proportion_cancelled)) +
geom_point() +
labs(title = "Prop of Cancelled Flights Per Month",
y = "Cancelled Proportion")
#a. looks like February had highest cancellation proportion
#b. and October had the lowest
Question #2
Consider the following pipeline:
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg)) %>%
filter(am == 1)
test_mtcars <- mtcars
test_mtcars$am <- as.factor(test_mtcars$am)
#2. filter applied after summarizing the data did not work. if we apply filter for transmission (1=manual, 0=automatic) before summarizing the avg miles per gallon, the code works
test_mtcars %>%
group_by(cyl) %>%
filter(am == 1) %>%
summarize(avg_mpg = mean(mpg))
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)
q3data <- Teams
added_variable_Teams <- q3data %>%
mutate(BA = H/AB) %>%
mutate(SLG = (H+2*X2B+3*X3B+4*HR)/AB)
##mutate would add columns to the data
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)
#MLB History
added_variable_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
added_variable_Teams %>%
filter(yearID >= 1969) %>%
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
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)
Batting <- Batting
Pitching <- Pitching
People <- People
#a
Batting %>%
group_by(playerID) %>%
summarize(Total_HR = sum(HR),Total_SB = sum(SB)) %>%
right_join(People, by = c("playerID" = "playerID")) %>%
filter(Total_HR >= 300 & Total_SB >= 300) %>%
select(nameFirst, nameLast, nameGiven, Total_HR, Total_SB)
## # A tibble: 8 × 5
## nameFirst nameLast nameGiven Total_HR Total_SB
## <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
#b
Pitching %>%
group_by(playerID) %>%
summarize(Total_W = sum(W),Total_SO = sum(SO)) %>%
right_join(People, by = c("playerID" = "playerID")) %>%
filter(Total_W >= 300 & Total_SO >= 3000) %>%
select(nameFirst, nameLast, nameGiven, Total_SO, Total_W)
## # A tibble: 10 × 5
## nameFirst nameLast nameGiven Total_SO Total_W
## <chr> <chr> <chr> <int> <int>
## 1 Steve Carlton Steven Norman 4136 329
## 2 Roger Clemens William Roger 4672 354
## 3 Randy Johnson Randall David 4875 303
## 4 Walter Johnson Walter Perry 3509 417
## 5 Greg Maddux Gregory Alan 3371 355
## 6 Phil Niekro Philip Henry 3342 318
## 7 Gaylord Perry Gaylord Jackson 3534 314
## 8 Nolan Ryan Lynn Nolan 5714 324
## 9 Tom Seaver George Thomas 3640 311
## 10 Don Sutton Donald Howard 3574 324
#c
Batting %>%
group_by(playerID, yearID) %>%
summarize(Total_HR = sum(HR), BA = sum(H)/sum(AB)) %>%
right_join(People, by = c("playerID" = "playerID")) %>%
filter(Total_HR >= 50) %>%
select(nameFirst, nameLast, nameGiven, yearID, BA, Total_HR) %>%
arrange(BA)
## # A tibble: 46 × 7
## # Groups: playerID [30]
## playerID nameFirst nameLast nameGiven yearID BA Total_HR
## <chr> <chr> <chr> <chr> <int> <dbl> <int>
## 1 alonspe01 Pete Alonso Peter Morgan 2019 0.260 53
## 2 bautijo02 Jose Bautista Jose Antonio 2010 0.260 54
## 3 jonesan01 Andruw Jones Andruw Rudolf 2005 0.263 51
## 4 marisro01 Roger Maris Roger Eugene 1961 0.269 61
## 5 vaughgr01 Greg Vaughn Gregory Lamont 1998 0.272 50
## 6 mcgwima01 Mark McGwire Mark David 1997 0.274 58
## 7 fieldce01 Cecil Fielder Cecil Grant 1990 0.277 51
## 8 mcgwima01 Mark McGwire Mark David 1999 0.278 65
## 9 stantmi03 Giancarlo Stanton Giancarlo Cruz-Michael 2017 0.281 59
## 10 judgeaa01 Aaron Judge Aaron James 2017 0.284 52
## # … with 36 more rows