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? #February had the highest proportion of cancelled flights b.What month had the lowest? #October had the lowest proportion of cancelled flights
library(nycflights13)
glimpse(flights)
## Rows: 336,776
## Columns: 19
## $ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2…
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ dep_time <int> 517, 533, 542, 544, 554, 554, 555, 557, 557, 558, 558, …
## $ sched_dep_time <int> 515, 529, 540, 545, 600, 558, 600, 600, 600, 600, 600, …
## $ dep_delay <dbl> 2, 4, 2, -1, -6, -4, -5, -3, -3, -2, -2, -2, -2, -2, -1…
## $ arr_time <int> 830, 850, 923, 1004, 812, 740, 913, 709, 838, 753, 849,…
## $ sched_arr_time <int> 819, 830, 850, 1022, 837, 728, 854, 723, 846, 745, 851,…
## $ arr_delay <dbl> 11, 20, 33, -18, -25, 12, 19, -14, -8, 8, -2, -3, 7, -1…
## $ carrier <chr> "UA", "UA", "AA", "B6", "DL", "UA", "B6", "EV", "B6", "…
## $ flight <int> 1545, 1714, 1141, 725, 461, 1696, 507, 5708, 79, 301, 4…
## $ tailnum <chr> "N14228", "N24211", "N619AA", "N804JB", "N668DN", "N394…
## $ origin <chr> "EWR", "LGA", "JFK", "JFK", "LGA", "EWR", "EWR", "LGA",…
## $ dest <chr> "IAH", "IAH", "MIA", "BQN", "ATL", "ORD", "FLL", "IAD",…
## $ air_time <dbl> 227, 227, 160, 183, 116, 150, 158, 53, 140, 138, 149, 1…
## $ distance <dbl> 1400, 1416, 1089, 1576, 762, 719, 1065, 229, 944, 733, …
## $ hour <dbl> 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6…
## $ minute <dbl> 15, 29, 40, 45, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0…
## $ time_hour <dttm> 2013-01-01 05:00:00, 2013-01-01 05:00:00, 2013-01-01 0…
flights_new <- flights %>%
group_by(month) %>%
summarize(cancelled_flights = sum(is.na(arr_delay)),
total_flights = n(),
proportion_cancelled = cancelled_flights/total_flights) %>%
arrange(desc(proportion_cancelled))
flights_new
## # A tibble: 12 × 4
## month cancelled_flights total_flights proportion_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
Question #2
Consider the following pipeline:
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg)) %>%
filter(am == 1)
#code_fix library(tidyverse) mtcars %>% group_by(cyl) %>% filter(am == 1) summarize(avg_mpg = mean(mpg)) %>%
What is the problem with this pipeline? #Error caused by
mask$eval_all_filter()
:! object ‘am’ not found. #To fix
this, I moved the “filter” function before the “summarize” function.
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)
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
Teams %>%
filter(yearID >= 1969) %>%
select(yearID, teamID, SLG) %>%
arrange(desc(SLG)) %>%
head(15)
## 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
## 6 2020 LAN 0.5910872
## 7 1997 SEA 0.5908443
## 8 1996 SEA 0.5906845
## 9 1994 CLE 0.5900050
## 10 2001 COL 0.5880492
## 11 2017 HOU 0.5854571
## 12 2009 NYA 0.5818021
## 13 2019 LAN 0.5814673
## 14 2004 BOS 0.5807692
## 15 1995 CLE 0.5799523
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)
#a.1.
Batting_5_A_1 <-
Batting %>%
group_by(playerID)%>%
summarize(total_HR = sum(HR),
total_SB = sum(SB)) %>%
filter(total_HR >= 300 & total_SB >= 300) %>%
left_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, total_HR, total_SB) %>%
arrange(desc(total_HR))
Batting_5_A_1
## # A tibble: 8 × 4
## nameFirst nameLast total_HR total_SB
## <chr> <chr> <int> <int>
## 1 Barry Bonds 762 514
## 2 Alex Rodriguez 696 329
## 3 Willie Mays 660 338
## 4 Andre Dawson 438 314
## 5 Carlos Beltran 435 312
## 6 Bobby Bonds 332 461
## 7 Reggie Sanders 305 304
## 8 Steve Finley 304 320
#a.2.
Pitching_5_A_2 <-
Pitching %>%
group_by(playerID)%>%
summarize(total_W = sum(W),
total_SO = sum(SO)) %>%
filter(total_W >= 300 & total_SO >= 3000) %>%
left_join(People, by = c('playerID' = 'playerID')) %>%
select(nameFirst, nameLast, total_W , total_SO) %>%
arrange(desc(total_W))
Pitching_5_A_2
## # A tibble: 10 × 4
## nameFirst nameLast total_W total_SO
## <chr> <chr> <int> <int>
## 1 Walter Johnson 417 3509
## 2 Greg Maddux 355 3371
## 3 Roger Clemens 354 4672
## 4 Steve Carlton 329 4136
## 5 Nolan Ryan 324 5714
## 6 Don Sutton 324 3574
## 7 Phil Niekro 318 3342
## 8 Gaylord Perry 314 3534
## 9 Tom Seaver 311 3640
## 10 Randy Johnson 303 4875
#b.1.
Batting_5_B_1 <-
Batting %>%
group_by(playerID, yearID)%>%
summarize(total_HR = sum(HR),
batting_avg = sum(H)/sum(AB)) %>%
filter(total_HR >= 50) %>%
left_join(People, by = c('playerID' = 'playerID')) %>%
select(yearID, nameFirst, nameLast, total_HR, batting_avg) %>%
arrange(batting_avg)
Batting_5_B_1
## # A tibble: 46 × 6
## # Groups: playerID [30]
## playerID yearID nameFirst nameLast total_HR batting_avg
## <chr> <int> <chr> <chr> <int> <dbl>
## 1 alonspe01 2019 Pete Alonso 53 0.260
## 2 bautijo02 2010 Jose Bautista 54 0.260
## 3 jonesan01 2005 Andruw Jones 51 0.263
## 4 marisro01 1961 Roger Maris 61 0.269
## 5 vaughgr01 1998 Greg Vaughn 50 0.272
## 6 mcgwima01 1997 Mark McGwire 58 0.274
## 7 fieldce01 1990 Cecil Fielder 51 0.277
## 8 mcgwima01 1999 Mark McGwire 65 0.278
## 9 stantmi03 2017 Giancarlo Stanton 59 0.281
## 10 judgeaa01 2017 Aaron Judge 52 0.284
## # … with 36 more rows
#b.2
Batting_5.B_2 <-
Batting %>%
group_by(playerID, yearID, HR)%>%
summarize(batting_avg = sum(H)/sum(AB)) %>%
filter(HR >= 50 & yearID == 2001) %>%
left_join(People, by = c('playerID' = 'playerID')) %>%
select(yearID, nameFirst, nameLast, HR, batting_avg)
Batting_5.B_2
## # A tibble: 4 × 6
## # Groups: playerID, yearID [4]
## playerID yearID nameFirst nameLast HR batting_avg
## <chr> <int> <chr> <chr> <int> <dbl>
## 1 bondsba01 2001 Barry Bonds 73 0.328
## 2 gonzalu01 2001 Luis Gonzalez 57 0.325
## 3 rodrial01 2001 Alex Rodriguez 52 0.318
## 4 sosasa01 2001 Sammy Sosa 64 0.328