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.
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? November
library(nycflights13)
flights <- flights
summary(flights)
## year month day dep_time sched_dep_time
## Min. :2013 Min. : 1.000 Min. : 1.00 Min. : 1 Min. : 106
## 1st Qu.:2013 1st Qu.: 4.000 1st Qu.: 8.00 1st Qu.: 907 1st Qu.: 906
## Median :2013 Median : 7.000 Median :16.00 Median :1401 Median :1359
## Mean :2013 Mean : 6.549 Mean :15.71 Mean :1349 Mean :1344
## 3rd Qu.:2013 3rd Qu.:10.000 3rd Qu.:23.00 3rd Qu.:1744 3rd Qu.:1729
## Max. :2013 Max. :12.000 Max. :31.00 Max. :2400 Max. :2359
## NA's :8255
## dep_delay arr_time sched_arr_time arr_delay
## Min. : -43.00 Min. : 1 Min. : 1 Min. : -86.000
## 1st Qu.: -5.00 1st Qu.:1104 1st Qu.:1124 1st Qu.: -17.000
## Median : -2.00 Median :1535 Median :1556 Median : -5.000
## Mean : 12.64 Mean :1502 Mean :1536 Mean : 6.895
## 3rd Qu.: 11.00 3rd Qu.:1940 3rd Qu.:1945 3rd Qu.: 14.000
## Max. :1301.00 Max. :2400 Max. :2359 Max. :1272.000
## NA's :8255 NA's :8713 NA's :9430
## carrier flight tailnum origin
## Length:336776 Min. : 1 Length:336776 Length:336776
## Class :character 1st Qu.: 553 Class :character Class :character
## Mode :character Median :1496 Mode :character Mode :character
## Mean :1972
## 3rd Qu.:3465
## Max. :8500
##
## dest air_time distance hour
## Length:336776 Min. : 20.0 Min. : 17 Min. : 1.00
## Class :character 1st Qu.: 82.0 1st Qu.: 502 1st Qu.: 9.00
## Mode :character Median :129.0 Median : 872 Median :13.00
## Mean :150.7 Mean :1040 Mean :13.18
## 3rd Qu.:192.0 3rd Qu.:1389 3rd Qu.:17.00
## Max. :695.0 Max. :4983 Max. :23.00
## NA's :9430
## minute time_hour
## Min. : 0.00 Min. :2013-01-01 05:00:00.00
## 1st Qu.: 8.00 1st Qu.:2013-04-04 13:00:00.00
## Median :29.00 Median :2013-07-03 10:00:00.00
## Mean :26.23 Mean :2013-07-03 05:22:54.64
## 3rd Qu.:44.00 3rd Qu.:2013-10-01 07:00:00.00
## Max. :59.00 Max. :2013-12-31 23:00:00.00
##
flights$cancelled <- is.na(flights$dep_time) | is.na(flights$arr_time) | is.na(flights$arr_delay) | is.na(flights$air_time)
flights %>%
group_by(month) %>%
summarize(
Total_flights = n(),
Cancelled_flights = sum(cancelled),
Proportion_cancelled = Cancelled_flights/Total_flights
) %>%
kable(
caption = "Do children eat more when they help make the food?",
digits = c(0,0,0,4),
col.names = c("Month", "Total Flights", "Cancelled Flights", "Proportion Cancelled")
) %>%
kable_styling()
| Month | Total Flights | Cancelled Flights | Proportion Cancelled |
|---|---|---|---|
| 1 | 27004 | 606 | 0.0224 |
| 2 | 24951 | 1340 | 0.0537 |
| 3 | 28834 | 932 | 0.0323 |
| 4 | 28330 | 766 | 0.0270 |
| 5 | 28796 | 668 | 0.0232 |
| 6 | 28243 | 1168 | 0.0414 |
| 7 | 29425 | 1132 | 0.0385 |
| 8 | 29327 | 571 | 0.0195 |
| 9 | 27574 | 564 | 0.0205 |
| 10 | 28889 | 271 | 0.0094 |
| 11 | 27268 | 297 | 0.0109 |
| 12 | 28135 | 1115 | 0.0396 |
Consider the following pipeline:
data(mtcars)
mtcars %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg)) %>%
filter(am == 1)
What is the problem with this pipeline? This pipeline runs into error because you cannot filter for ‘am == 1’ after doing ‘group_by(cyl)’ and ‘summarize(avg_mpg = mean(mpg))’. Basically, after you summarize avg_mpg by calculating its average based on ‘cyl’ group, all am values are aggregated, so you cannot filter by ‘am == 1’ anymore.
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
Teams$BA <- Teams$H/Teams$AB
Teams$X1B <- Teams$H - Teams$X2B - Teams$X3B - Teams$HR
Teams$totalBases <- Teams$X1B + Teams$X2B*2 + Teams$X3B*3 + Teams$HR*4
Teams$SLG <- Teams$totalBases/Teams$AB
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.
In all of MLB history, the top 5 teams ranked by SLG are HOU, MIN, BOS, HYA, and SEA. After 1969, the top 5 teams ranked by SLG are still the these five teams, with the difference being the two SLG records set by HYA in 1927 and 1930 are excluded.
# We will use the SLG variable created in Question #3
# All of Major League Baseball History
Teams %>%
group_by(teamID) %>%
arrange(desc(SLG)) %>%
dplyr::select(yearID, teamID, SLG) %>%
top_n(10)
## Selecting by SLG
## # A tibble: 701 × 3
## # Groups: teamID [149]
## yearID teamID SLG
## <int> <fct> <dbl>
## 1 2019 HOU 0.495
## 2 2019 MIN 0.494
## 3 2003 BOS 0.491
## 4 2019 NYA 0.490
## 5 1927 NYA 0.489
## 6 1930 NYA 0.488
## 7 1997 SEA 0.485
## 8 1894 BSN 0.484
## 9 1994 CLE 0.484
## 10 1996 SEA 0.484
## # … with 691 more rows
# Since 1969
Teams %>%
filter(yearID >= 1969) %>%
group_by(teamID) %>%
arrange(desc(SLG)) %>%
dplyr::select(yearID, teamID, SLG) %>%
top_n(10)
## Selecting by SLG
## # A tibble: 352 × 3
## # Groups: teamID [37]
## yearID teamID SLG
## <int> <fct> <dbl>
## 1 2019 HOU 0.495
## 2 2019 MIN 0.494
## 3 2003 BOS 0.491
## 4 2019 NYA 0.490
## 5 1997 SEA 0.485
## 6 1994 CLE 0.484
## 7 1996 SEA 0.484
## 8 2001 COL 0.483
## 9 2020 LAN 0.483
## 10 2020 ATL 0.483
## # … with 342 more rows
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.
These 8 batters are: Carlos Beltran, Barry Bonds, Bobby Bonds, Andre Dawson, Steve Finley, Willie Mays, Alex Rodriguez and Reggie Sanders.
# Part a
Batting <- Batting
Pitching <- Pitching
People <- People
top_batter <- Batting %>%
group_by(playerID) %>%
summarise(
sum_HR = sum(HR),
sum_SB = sum(SB)
)
top_batter_2 <- top_batter %>%
filter(sum_HR > 300 & sum_SB > 300)
top_batter_joined <- top_batter_2 %>%
inner_join(People, by = c("playerID" = "playerID"))
top_batter_joined %>%
dplyr::select(playerID, nameFirst, nameLast, sum_HR, sum_SB)
## # A tibble: 8 × 5
## playerID nameFirst nameLast sum_HR sum_SB
## <chr> <chr> <chr> <int> <int>
## 1 beltrca01 Carlos Beltran 435 312
## 2 bondsba01 Barry Bonds 762 514
## 3 bondsbo01 Bobby Bonds 332 461
## 4 dawsoan01 Andre Dawson 438 314
## 5 finlest01 Steve Finley 304 320
## 6 mayswi01 Willie Mays 660 338
## 7 rodrial01 Alex Rodriguez 696 329
## 8 sandere02 Reggie Sanders 305 304
# Part b
top_pitcher <- Pitching %>%
group_by(playerID) %>%
summarise(
sum_W = sum(W),
sum_SO = sum(SO)
)
top_pitcher_2 <- top_pitcher %>%
filter(sum_W > 300 & sum_SO > 3000)
top_pitcher_joined <- top_pitcher_2 %>%
inner_join(People, by = c("playerID" = "playerID"))
top_pitcher_joined %>%
dplyr::select(playerID, nameFirst, nameLast, sum_W, sum_SO)
## # A tibble: 10 × 5
## playerID nameFirst nameLast sum_W sum_SO
## <chr> <chr> <chr> <int> <int>
## 1 carltst01 Steve Carlton 329 4136
## 2 clemero02 Roger Clemens 354 4672
## 3 johnsra05 Randy Johnson 303 4875
## 4 johnswa01 Walter Johnson 417 3509
## 5 maddugr01 Greg Maddux 355 3371
## 6 niekrph01 Phil Niekro 318 3342
## 7 perryga01 Gaylord Perry 314 3534
## 8 ryanno01 Nolan Ryan 324 5714
## 9 seaveto01 Tom Seaver 311 3640
## 10 suttodo01 Don Sutton 324 3574
The name and year of every player who has hit at least 50 HR are displayed in the table below. Pete Alonso has the lowest batting average of 0.259631 in year 2019.
# Part c
Batting$BA <- Batting$H/Batting$AB
batting2 <- Batting %>%
filter(HR > 50)
batting_joined <- batting2 %>%
inner_join(People, by = c("playerID" = "playerID"))
batting_joined %>%
dplyr::select(playerID, nameFirst, nameLast, yearID, HR, BA) %>%
arrange(BA)
## playerID nameFirst nameLast yearID HR BA
## 1 alonspe01 Pete Alonso 2019 53 0.2596315
## 2 bautijo02 Jose Bautista 2010 54 0.2601054
## 3 jonesan01 Andruw Jones 2005 51 0.2627986
## 4 marisro01 Roger Maris 1961 61 0.2694915
## 5 fieldce01 Cecil Fielder 1990 51 0.2774869
## 6 mcgwima01 Mark McGwire 1999 65 0.2783109
## 7 stantmi03 Giancarlo Stanton 2017 59 0.2814070
## 8 judgeaa01 Aaron Judge 2017 52 0.2841328
## 9 griffke02 Ken Griffey 1998 56 0.2843602
## 10 davisch02 Chris Davis 2013 53 0.2859589
## 11 ortizda01 David Ortiz 2006 54 0.2867384
## 12 sosasa01 Sammy Sosa 1999 63 0.2880000
## 13 mcgwima01 Mark McGwire 1998 70 0.2986248
## 14 rodrial01 Alex Rodriguez 2002 57 0.2996795
## 15 mizejo01 Johnny Mize 1947 51 0.3020478
## 16 thomeji01 Jim Thome 2002 52 0.3041667
## 17 griffke02 Ken Griffey 1997 56 0.3042763
## 18 sosasa01 Sammy Sosa 1998 66 0.3079316
## 19 kinerra01 Ralph Kiner 1949 54 0.3096539
## 20 mcgwima01 Mark McGwire 1996 52 0.3120567
## 21 howarry01 Ryan Howard 2006 58 0.3132530
## 22 kinerra01 Ralph Kiner 1947 51 0.3132743
## 23 rodrial01 Alex Rodriguez 2007 54 0.3138937
## 24 greenha01 Hank Greenberg 1938 58 0.3147482
## 25 mantlmi01 Mickey Mantle 1961 54 0.3171206
## 26 mayswi01 Willie Mays 1965 52 0.3172043
## 27 rodrial01 Alex Rodriguez 2001 52 0.3180380
## 28 mayswi01 Willie Mays 1955 51 0.3189655
## 29 fostege01 George Foster 1977 52 0.3203252
## 30 ruthba01 Babe Ruth 1928 54 0.3227612
## 31 gonzalu01 Luis Gonzalez 2001 57 0.3251232
## 32 sosasa01 Sammy Sosa 2001 64 0.3275563
## 33 bondsba01 Barry Bonds 2001 73 0.3277311
## 34 mantlmi01 Mickey Mantle 1956 52 0.3527205
## 35 ruthba01 Babe Ruth 1927 60 0.3555556
## 36 wilsoha01 Hack Wilson 1930 56 0.3555556
## 37 foxxji01 Jimmie Foxx 1932 58 0.3641026
## 38 ruthba01 Babe Ruth 1920 54 0.3763676
## 39 ruthba01 Babe Ruth 1921 59 0.3777778