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)
#data preparation
flights[is.na(flights$dep_time),]
## # A tibble: 8,255 × 19
## year month day dep_time sched_de…¹ dep_d…² arr_t…³ sched…⁴ arr_d…⁵ carrier
## <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <chr>
## 1 2013 1 1 NA 1630 NA NA 1815 NA EV
## 2 2013 1 1 NA 1935 NA NA 2240 NA AA
## 3 2013 1 1 NA 1500 NA NA 1825 NA AA
## 4 2013 1 1 NA 600 NA NA 901 NA B6
## 5 2013 1 2 NA 1540 NA NA 1747 NA EV
## 6 2013 1 2 NA 1620 NA NA 1746 NA EV
## 7 2013 1 2 NA 1355 NA NA 1459 NA EV
## 8 2013 1 2 NA 1420 NA NA 1644 NA EV
## 9 2013 1 2 NA 1321 NA NA 1536 NA EV
## 10 2013 1 2 NA 1545 NA NA 1910 NA AA
## # … with 8,245 more rows, 9 more variables: flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm>, and abbreviated variable names
## # ¹sched_dep_time, ²dep_delay, ³arr_time, ⁴sched_arr_time, ⁵arr_delay
#formula build up
cancelled_flights_by_month = flights %>%
group_by(month) %>%
summarize(num_cancelled = sum(is.na(dep_time)),
ratio_cancelled = num_cancelled/n())
#a
cancelled_flights_by_month[which.max(cancelled_flights_by_month$ratio_cancelled),]
## # A tibble: 1 × 3
## month num_cancelled ratio_cancelled
## <int> <int> <dbl>
## 1 2 1261 0.0505
#b
cancelled_flights_by_month[which.min(cancelled_flights_by_month$ratio_cancelled),]
## # A tibble: 1 × 3
## month num_cancelled ratio_cancelled
## <int> <int> <dbl>
## 1 10 236 0.00817
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 issue is applying filter after aggregation. After the summarize, the data frame is left with only cyl and avg_mpg columns. AM cloumn no longer exists.
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)
#a
Teams = Teams %>%
mutate(BA = H / AB)
summary(Teams$BA)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.1564 0.2494 0.2600 0.2607 0.2708 0.3498
#b
Teams = Teams %>%
mutate(SLG = (H + 2 * X2B + 3 * X3B + 4 * HR) / AB)
summary(Teams$SLG)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.1659 0.4192 0.4596 0.4561 0.4950 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. See R results below
#top 5 ranked by SLG
Teams %>%
select(name, yearID, SLG) %>%
arrange(desc(SLG)) %>%
head(5)
## name yearID SLG
## 1 Houston Astros 2019 0.6092998
## 2 Minnesota Twins 2019 0.6071179
## 3 Boston Red Sox 2003 0.6033975
## 4 New York Yankees 2019 0.5996776
## 5 Atlanta Braves 2020 0.5964320
#Top 5 ranked by SLG when YEAR >= 1969
Teams %>%
select(name, yearID, SLG) %>%
filter(yearID >= 1969) %>%
arrange(desc(SLG)) %>%
head(5)
## name yearID SLG
## 1 Houston Astros 2019 0.6092998
## 2 Minnesota Twins 2019 0.6071179
## 3 Boston Red Sox 2003 0.6033975
## 4 New York Yankees 2019 0.5996776
## 5 Atlanta Braves 2020 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.See Results
Similarly, name every pitcher in baseball history who has accumulated at least 300 wins (W) and at least 3,000 strikeouts (SO).See Results
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? Pete Alonso
library(Lahman)
#a
Batting %>%
group_by(playerID) %>%
summarize(total_home_runs = sum(HR), total_stolen_bases = sum (SB)) %>%
filter(total_home_runs >= 300 & total_stolen_bases >= 300) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, total_home_runs, total_stolen_bases)
## # A tibble: 8 × 4
## nameFirst nameLast total_home_runs total_stolen_bases
## <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(total_wins = sum(W), total_strikeouts = sum(SO)) %>%
filter(total_wins >= 300 & total_strikeouts >= 3000) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, nameGiven, total_wins, total_strikeouts)
## # A tibble: 10 × 5
## nameFirst nameLast nameGiven total_wins total_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
#c
players = Batting %>%
group_by(playerID, yearID) %>%
summarize(total_home_runs = sum(HR), batting_avg = sum(H) / sum(AB)) %>%
filter(total_home_runs >= 50) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(yearID, nameFirst, nameLast, total_home_runs, batting_avg) %>%
arrange(batting_avg)
players[which.min(players$batting_avg),]
## # A tibble: 1 × 6
## # Groups: playerID [1]
## playerID yearID nameFirst nameLast total_home_runs batting_avg
## <chr> <int> <chr> <chr> <int> <dbl>
## 1 alonspe01 2019 Pete Alonso 53 0.260