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)
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…
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 %>%
group_by(month) %>%
summarise(canc_prop = (sum(is.na(dep_time))/n()) * 100) %>%
arrange(desc(canc_prop))
## # A tibble: 12 × 2
## month canc_prop
## <int> <dbl>
## 1 2 5.05
## 2 12 3.64
## 3 6 3.57
## 4 7 3.19
## 5 3 2.99
## 6 4 2.36
## 7 5 1.96
## 8 1 1.93
## 9 8 1.66
## 10 9 1.64
## 11 11 0.854
## 12 10 0.817
Question #2
Consider the following pipeline:
library(tidyverse)
summary(mtcars)
mtcars %>%
group_by(cyl) %>%
summarize(avg_mpg = mean(mpg)) %>%
filter(am == 1)
What is the problem with this pipeline?
mtcars %>%
group_by(cyl) %>%
filter(am == 1) %>%
summarize(avg_mpg = mean(mpg))
Filter being after the summarizing part of pipeline caused the problem. Moving the filter piece between grouping and summarizing parts helped to solve the issue.
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)
head(Teams)
## yearID lgID teamID franchID divID Rank G Ghome W L DivWin WCWin LgWin
## 1 1871 NA BS1 BNA <NA> 3 31 NA 20 10 <NA> <NA> N
## 2 1871 NA CH1 CNA <NA> 2 28 NA 19 9 <NA> <NA> N
## 3 1871 NA CL1 CFC <NA> 8 29 NA 10 19 <NA> <NA> N
## 4 1871 NA FW1 KEK <NA> 7 19 NA 7 12 <NA> <NA> N
## 5 1871 NA NY2 NNA <NA> 5 33 NA 16 17 <NA> <NA> N
## 6 1871 NA PH1 PNA <NA> 1 28 NA 21 7 <NA> <NA> Y
## WSWin R AB H X2B X3B HR BB SO SB CS HBP SF RA ER ERA CG SHO SV
## 1 <NA> 401 1372 426 70 37 3 60 19 73 16 NA NA 303 109 3.55 22 1 3
## 2 <NA> 302 1196 323 52 21 10 60 22 69 21 NA NA 241 77 2.76 25 0 1
## 3 <NA> 249 1186 328 35 40 7 26 25 18 8 NA NA 341 116 4.11 23 0 0
## 4 <NA> 137 746 178 19 8 2 33 9 16 4 NA NA 243 97 5.17 19 1 0
## 5 <NA> 302 1404 403 43 21 1 33 15 46 15 NA NA 313 121 3.72 32 1 0
## 6 <NA> 376 1281 410 66 27 9 46 23 56 12 NA NA 266 137 4.95 27 0 0
## IPouts HA HRA BBA SOA E DP FP name
## 1 828 367 2 42 23 243 24 0.834 Boston Red Stockings
## 2 753 308 6 28 22 229 16 0.829 Chicago White Stockings
## 3 762 346 13 53 34 234 15 0.818 Cleveland Forest Citys
## 4 507 261 5 21 17 163 8 0.803 Fort Wayne Kekiongas
## 5 879 373 7 42 22 235 14 0.840 New York Mutuals
## 6 747 329 3 53 16 194 13 0.845 Philadelphia Athletics
## park attendance BPF PPF teamIDBR teamIDlahman45
## 1 South End Grounds I NA 103 98 BOS BS1
## 2 Union Base-Ball Grounds NA 104 102 CHI CH1
## 3 National Association Grounds NA 96 100 CLE CL1
## 4 Hamilton Field NA 101 107 KEK FW1
## 5 Union Grounds (Brooklyn) NA 90 88 NYU NY2
## 6 Jefferson Street Grounds NA 102 98 ATH PH1
## teamIDretro
## 1 BS1
## 2 CH1
## 3 CL1
## 4 FW1
## 5 NY2
## 6 PH1
summary(Teams)
## yearID lgID teamID franchID divID
## Min. :1871 AA: 85 CHN : 146 ATL : 146 Length:2985
## 1st Qu.:1922 AL:1295 PHI : 139 CHC : 146 Class :character
## Median :1967 FL: 16 PIT : 135 CIN : 140 Mode :character
## Mean :1959 NA: 50 CIN : 132 PIT : 140
## 3rd Qu.:1997 NL:1519 SLN : 130 STL : 140
## Max. :2021 PL: 8 BOS : 121 PHI : 139
## UA: 12 (Other):2182 (Other):2134
## Rank G Ghome W
## Min. : 1.000 Min. : 6 Min. :24.00 Min. : 0.00
## 1st Qu.: 2.000 1st Qu.:154 1st Qu.:77.00 1st Qu.: 66.00
## Median : 4.000 Median :159 Median :81.00 Median : 77.00
## Mean : 4.039 Mean :150 Mean :78.05 Mean : 74.61
## 3rd Qu.: 6.000 3rd Qu.:162 3rd Qu.:81.00 3rd Qu.: 87.00
## Max. :13.000 Max. :165 Max. :84.00 Max. :116.00
## NA's :399
## L DivWin WCWin LgWin
## Min. : 4.00 Length:2985 Length:2985 Length:2985
## 1st Qu.: 65.00 Class :character Class :character Class :character
## Median : 76.00 Mode :character Mode :character Mode :character
## Mean : 74.61
## 3rd Qu.: 87.00
## Max. :134.00
##
## WSWin R AB H
## Length:2985 Min. : 24 Min. : 211 Min. : 33
## Class :character 1st Qu.: 614 1st Qu.:5135 1st Qu.:1299
## Mode :character Median : 691 Median :5402 Median :1390
## Mean : 681 Mean :5129 Mean :1339
## 3rd Qu.: 764 3rd Qu.:5519 3rd Qu.:1465
## Max. :1220 Max. :5781 Max. :1783
##
## X2B X3B HR BB
## Min. : 1.0 Min. : 0.00 Min. : 0.0 Min. : 1.0
## 1st Qu.:194.0 1st Qu.: 29.00 1st Qu.: 45.0 1st Qu.:425.8
## Median :234.0 Median : 40.00 Median :110.0 Median :494.0
## Mean :228.7 Mean : 45.67 Mean :105.9 Mean :473.6
## 3rd Qu.:272.0 3rd Qu.: 59.00 3rd Qu.:155.0 3rd Qu.:554.2
## Max. :376.0 Max. :150.00 Max. :307.0 Max. :835.0
## NA's :1
## SO SB CS HBP
## Min. : 3.0 Min. : 1.0 Min. : 3.00 Min. : 7.00
## 1st Qu.: 516.0 1st Qu.: 62.5 1st Qu.: 33.00 1st Qu.: 32.00
## Median : 761.0 Median : 93.0 Median : 44.00 Median : 43.00
## Mean : 762.1 Mean :109.4 Mean : 46.55 Mean : 45.82
## 3rd Qu.: 990.0 3rd Qu.:137.0 3rd Qu.: 56.00 3rd Qu.: 57.00
## Max. :1596.0 Max. :581.0 Max. :191.00 Max. :160.00
## NA's :16 NA's :126 NA's :832 NA's :1158
## SF RA ER ERA
## Min. : 7.00 Min. : 34 Min. : 23.0 Min. :1.220
## 1st Qu.:38.00 1st Qu.: 610 1st Qu.: 503.0 1st Qu.:3.370
## Median :44.00 Median : 689 Median : 594.0 Median :3.840
## Mean :44.11 Mean : 681 Mean : 573.4 Mean :3.841
## 3rd Qu.:50.00 3rd Qu.: 766 3rd Qu.: 671.0 3rd Qu.:4.330
## Max. :77.00 Max. :1252 Max. :1023.0 Max. :8.000
## NA's :1541
## CG SHO SV IPouts
## Min. : 0.00 Min. : 0.000 Min. : 0.00 Min. : 162
## 1st Qu.: 9.00 1st Qu.: 6.000 1st Qu.:10.00 1st Qu.:4080
## Median : 41.00 Median : 9.000 Median :25.00 Median :4252
## Mean : 47.55 Mean : 9.588 Mean :24.42 Mean :4013
## 3rd Qu.: 76.00 3rd Qu.:12.000 3rd Qu.:39.00 3rd Qu.:4341
## Max. :148.00 Max. :32.000 Max. :68.00 Max. :4518
##
## HA HRA BBA SOA
## Min. : 49 Min. : 0.0 Min. : 1.0 Min. : 0.0
## 1st Qu.:1287 1st Qu.: 51.0 1st Qu.:429.0 1st Qu.: 511.0
## Median :1389 Median :113.0 Median :495.0 Median : 762.0
## Mean :1339 Mean :105.9 Mean :473.7 Mean : 761.6
## 3rd Qu.:1468 3rd Qu.:153.0 3rd Qu.:554.0 3rd Qu.: 997.0
## Max. :1993 Max. :305.0 Max. :827.0 Max. :1687.0
##
## E DP FP name
## Min. : 20.0 Min. : 0.0 Min. :0.7610 Length:2985
## 1st Qu.:111.0 1st Qu.:116.0 1st Qu.:0.9660 Class :character
## Median :141.0 Median :140.0 Median :0.9770 Mode :character
## Mean :180.8 Mean :132.6 Mean :0.9664
## 3rd Qu.:207.0 3rd Qu.:157.0 3rd Qu.:0.9810
## Max. :639.0 Max. :217.0 Max. :0.9910
##
## park attendance BPF PPF
## Length:2985 Min. : 0 Min. : 60.0 Min. : 60.0
## Class :character 1st Qu.: 538461 1st Qu.: 97.0 1st Qu.: 97.0
## Mode :character Median :1190886 Median :100.0 Median :100.0
## Mean :1376599 Mean :100.2 Mean :100.2
## 3rd Qu.:2066598 3rd Qu.:103.0 3rd Qu.:103.0
## Max. :4483350 Max. :129.0 Max. :141.0
## NA's :279
## teamIDBR teamIDlahman45 teamIDretro
## Length:2985 Length:2985 Length:2985
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
##
Teams = mutate(Teams, BA=H/AB)
Teams = mutate(Teams, SLG=(H+2*X2B+3*X3B+4*HR)/AB)
colnames(Teams)
## [1] "yearID" "lgID" "teamID" "franchID"
## [5] "divID" "Rank" "G" "Ghome"
## [9] "W" "L" "DivWin" "WCWin"
## [13] "LgWin" "WSWin" "R" "AB"
## [17] "H" "X2B" "X3B" "HR"
## [21] "BB" "SO" "SB" "CS"
## [25] "HBP" "SF" "RA" "ER"
## [29] "ERA" "CG" "SHO" "SV"
## [33] "IPouts" "HA" "HRA" "BBA"
## [37] "SOA" "E" "DP" "FP"
## [41] "name" "park" "attendance" "BPF"
## [45] "PPF" "teamIDBR" "teamIDlahman45" "teamIDretro"
## [49] "BA" "SLG"
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(teamID,SLG,yearID) %>%
arrange(desc(SLG)) %>%
head(5)
## teamID SLG yearID
## 1 HOU 0.6092998 2019
## 2 MIN 0.6071179 2019
## 3 BOS 0.6033975 2003
## 4 NYA 0.5996776 2019
## 5 ATL 0.5964320 2020
Teams %>%
filter(yearID>=1969) %>%
select(teamID,SLG,yearID) %>%
arrange(desc(SLG)) %>%
head(5)
## teamID SLG yearID
## 1 HOU 0.6092998 2019
## 2 MIN 0.6071179 2019
## 3 BOS 0.6033975 2003
## 4 NYA 0.5996776 2019
## 5 ATL 0.5964320 2020
Adding the filter didn’t change results as the top-5 teams by SLG are all from the 21st century.
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)
head(Batting)
## playerID yearID stint teamID lgID G AB R H X2B X3B HR RBI SB CS BB SO
## 1 abercda01 1871 1 TRO NA 1 4 0 0 0 0 0 0 0 0 0 0
## 2 addybo01 1871 1 RC1 NA 25 118 30 32 6 0 0 13 8 1 4 0
## 3 allisar01 1871 1 CL1 NA 29 137 28 40 4 5 0 19 3 1 2 5
## 4 allisdo01 1871 1 WS3 NA 27 133 28 44 10 2 2 27 1 1 0 2
## 5 ansonca01 1871 1 RC1 NA 25 120 29 39 11 3 0 16 6 2 2 1
## 6 armstbo01 1871 1 FW1 NA 12 49 9 11 2 1 0 5 0 1 0 1
## IBB HBP SH SF GIDP
## 1 NA NA NA NA 0
## 2 NA NA NA NA 0
## 3 NA NA NA NA 1
## 4 NA NA NA NA 0
## 5 NA NA NA NA 0
## 6 NA NA NA NA 0
head(Pitching)
## playerID yearID stint teamID lgID W L G GS CG SHO SV IPouts H ER HR BB
## 1 bechtge01 1871 1 PH1 NA 1 2 3 3 2 0 0 78 43 23 0 11
## 2 brainas01 1871 1 WS3 NA 12 15 30 30 30 0 0 792 361 132 4 37
## 3 fergubo01 1871 1 NY2 NA 0 0 1 0 0 0 0 3 8 3 0 0
## 4 fishech01 1871 1 RC1 NA 4 16 24 24 22 1 0 639 295 103 3 31
## 5 fleetfr01 1871 1 NY2 NA 0 1 1 1 1 0 0 27 20 10 0 3
## 6 flowedi01 1871 1 TRO NA 0 0 1 0 0 0 0 3 1 0 0 0
## SO BAOpp ERA IBB WP HBP BK BFP GF R SH SF GIDP
## 1 1 NA 7.96 NA 7 NA 0 146 0 42 NA NA NA
## 2 13 NA 4.50 NA 7 NA 0 1291 0 292 NA NA NA
## 3 0 NA 27.00 NA 2 NA 0 14 0 9 NA NA NA
## 4 15 NA 4.35 NA 20 NA 0 1080 1 257 NA NA NA
## 5 0 NA 10.00 NA 0 NA 0 57 0 21 NA NA NA
## 6 0 NA 0.00 NA 0 NA 0 3 1 0 NA NA NA
head(People)
## playerID birthYear birthMonth birthDay birthCountry birthState birthCity
## 1 aardsda01 1981 12 27 USA CO Denver
## 2 aaronha01 1934 2 5 USA AL Mobile
## 3 aaronto01 1939 8 5 USA AL Mobile
## 4 aasedo01 1954 9 8 USA CA Orange
## 5 abadan01 1972 8 25 USA FL Palm Beach
## 6 abadfe01 1985 12 17 D.R. La Romana La Romana
## deathYear deathMonth deathDay deathCountry deathState deathCity nameFirst
## 1 NA NA NA <NA> <NA> <NA> David
## 2 2021 1 22 USA GA Atlanta Hank
## 3 1984 8 16 USA GA Atlanta Tommie
## 4 NA NA NA <NA> <NA> <NA> Don
## 5 NA NA NA <NA> <NA> <NA> Andy
## 6 NA NA NA <NA> <NA> <NA> Fernando
## nameLast nameGiven weight height bats throws debut finalGame
## 1 Aardsma David Allan 215 75 R R 2004-04-06 2015-08-23
## 2 Aaron Henry Louis 180 72 R R 1954-04-13 1976-10-03
## 3 Aaron Tommie Lee 190 75 R R 1962-04-10 1971-09-26
## 4 Aase Donald William 190 75 R R 1977-07-26 1990-10-03
## 5 Abad Fausto Andres 184 73 L L 2001-09-10 2006-04-13
## 6 Abad Fernando Antonio 235 74 L L 2010-07-28 2021-10-01
## retroID bbrefID deathDate birthDate
## 1 aardd001 aardsda01 <NA> 1981-12-27
## 2 aaroh101 aaronha01 2021-01-22 1934-02-05
## 3 aarot101 aaronto01 1984-08-16 1939-08-05
## 4 aased001 aasedo01 <NA> 1954-09-08
## 5 abada001 abadan01 <NA> 1972-08-25
## 6 abadf001 abadfe01 <NA> 1985-12-17
summary(Batting)
## playerID yearID stint teamID lgID
## Length:110495 Min. :1871 Min. :1.00 CHN : 5129 AA: 1893
## Class :character 1st Qu.:1938 1st Qu.:1.00 PHI : 5026 AL:50965
## Mode :character Median :1977 Median :1.00 PIT : 4984 FL: 472
## Mean :1968 Mean :1.08 SLN : 4904 NA: 737
## 3rd Qu.:2002 3rd Qu.:1.00 CIN : 4786 NL:55945
## Max. :2021 Max. :5.00 CLE : 4731 PL: 149
## (Other):80935 UA: 334
## G AB R H
## Min. : 1.00 Min. : 0.0 Min. : 0.0 Min. : 0.00
## 1st Qu.: 12.00 1st Qu.: 3.0 1st Qu.: 0.0 1st Qu.: 0.00
## Median : 34.00 Median : 45.0 Median : 4.0 Median : 8.00
## Mean : 50.61 Mean :138.6 Mean : 18.4 Mean : 36.18
## 3rd Qu.: 78.00 3rd Qu.:222.0 3rd Qu.: 26.0 3rd Qu.: 55.00
## Max. :165.00 Max. :716.0 Max. :198.0 Max. :262.00
##
## X2B X3B HR RBI
## Min. : 0.000 Min. : 0.000 Min. : 0.00 Min. : 0.00
## 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.00 1st Qu.: 0.00
## Median : 1.000 Median : 0.000 Median : 0.00 Median : 3.00
## Mean : 6.177 Mean : 1.234 Mean : 2.86 Mean : 16.72
## 3rd Qu.: 9.000 3rd Qu.: 1.000 3rd Qu.: 2.00 3rd Qu.: 24.00
## Max. :67.000 Max. :36.000 Max. :73.00 Max. :191.00
## NA's :756
## SB CS BB SO
## Min. : 0.000 Min. : 0.000 Min. : 0.00 Min. : 0.00
## 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.00 1st Qu.: 1.00
## Median : 0.000 Median : 0.000 Median : 2.00 Median : 9.00
## Mean : 2.893 Mean : 1.162 Mean : 12.79 Mean : 20.62
## 3rd Qu.: 2.000 3rd Qu.: 1.000 3rd Qu.: 18.00 3rd Qu.: 29.00
## Max. :138.000 Max. :42.000 Max. :232.00 Max. :223.00
## NA's :2368 NA's :23541 NA's :2100
## IBB HBP SH SF
## Min. : 0.00 Min. : 0.000 Min. : 0.000 Min. : 0.00
## 1st Qu.: 0.00 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.00
## Median : 0.00 Median : 0.000 Median : 0.000 Median : 0.00
## Mean : 1.04 Mean : 1.061 Mean : 2.169 Mean : 1.01
## 3rd Qu.: 1.00 3rd Qu.: 1.000 3rd Qu.: 3.000 3rd Qu.: 1.00
## Max. :120.00 Max. :51.000 Max. :67.000 Max. :19.00
## NA's :36650 NA's :2816 NA's :6068 NA's :36103
## GIDP
## Min. : 0.000
## 1st Qu.: 0.000
## Median : 0.000
## Mean : 2.875
## 3rd Qu.: 4.000
## Max. :36.000
## NA's :25441
summary(Pitching)
## playerID yearID stint teamID lgID
## Length:49430 Min. :1871 Min. :1.000 PHI : 2277 AA: 657
## Class :character 1st Qu.:1946 1st Qu.:1.000 CHN : 2225 AL:23279
## Mode :character Median :1984 Median :1.000 PIT : 2192 FL: 173
## Mean :1973 Mean :1.082 SLN : 2187 NA: 132
## 3rd Qu.:2006 3rd Qu.:1.000 CIN : 2119 NL:25035
## Max. :2021 Max. :5.000 CLE : 2096 PL: 58
## (Other):36334 UA: 96
## W L G GS
## Min. : 0.000 Min. : 0.000 Min. : 1.00 Min. : 0.000
## 1st Qu.: 0.000 1st Qu.: 1.000 1st Qu.: 7.00 1st Qu.: 0.000
## Median : 2.000 Median : 3.000 Median : 21.00 Median : 2.000
## Mean : 4.504 Mean : 4.504 Mean : 23.42 Mean : 9.059
## 3rd Qu.: 7.000 3rd Qu.: 7.000 3rd Qu.: 34.00 3rd Qu.:16.000
## Max. :60.000 Max. :48.000 Max. :106.00 Max. :75.000
##
## CG SHO SV IPouts
## Min. : 0.000 Min. : 0.0000 Min. : 0.000 Min. : 0.0
## 1st Qu.: 0.000 1st Qu.: 0.0000 1st Qu.: 0.000 1st Qu.: 46.0
## Median : 0.000 Median : 0.0000 Median : 0.000 Median : 155.0
## Mean : 2.871 Mean : 0.4104 Mean : 1.475 Mean : 242.4
## 3rd Qu.: 2.000 3rd Qu.: 0.0000 3rd Qu.: 1.000 3rd Qu.: 370.0
## Max. :75.000 Max. :16.0000 Max. :62.000 Max. :2040.0
##
## H ER HR BB
## Min. : 0.00 Min. : 0.00 Min. : 0.000 Min. : 0.00
## 1st Qu.: 17.00 1st Qu.: 9.00 1st Qu.: 1.000 1st Qu.: 7.00
## Median : 51.00 Median : 23.00 Median : 4.000 Median : 20.00
## Mean : 80.87 Mean : 34.67 Mean : 6.393 Mean : 28.61
## 3rd Qu.:125.00 3rd Qu.: 55.00 3rd Qu.: 9.000 3rd Qu.: 43.00
## Max. :772.00 Max. :291.00 Max. :50.000 Max. :289.00
##
## SO BAOpp ERA IBB
## Min. : 0.00 Min. :0.000 Min. : 0.000 Min. : 0.000
## 1st Qu.: 8.00 1st Qu.:0.238 1st Qu.: 3.150 1st Qu.: 0.000
## Median : 30.00 Median :0.265 Median : 4.160 Median : 1.000
## Mean : 45.99 Mean :0.311 Mean : 5.152 Mean : 2.205
## 3rd Qu.: 67.00 3rd Qu.:0.300 3rd Qu.: 5.580 3rd Qu.: 3.000
## Max. :513.00 Max. :9.990 Max. :189.000 Max. :23.000
## NA's :4441 NA's :97 NA's :14578
## WP HBP BK BFP
## Min. : 0.000 Min. : 0.000 Min. : 0.0000 Min. : 0.0
## 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 0.0000 1st Qu.: 70.0
## Median : 1.000 Median : 1.000 Median : 0.0000 Median : 225.0
## Mean : 2.559 Mean : 2.351 Mean : 0.2896 Mean : 345.8
## 3rd Qu.: 4.000 3rd Qu.: 3.000 3rd Qu.: 0.0000 3rd Qu.: 529.0
## Max. :83.000 Max. :54.000 Max. :16.0000 Max. :2906.0
## NA's :734 NA's :3
## GF R SH SF
## Min. : 0.000 Min. : 0.00 Min. : 0.000 Min. : 0.000
## 1st Qu.: 0.000 1st Qu.: 10.00 1st Qu.: 0.000 1st Qu.: 0.000
## Median : 2.000 Median : 26.00 Median : 1.000 Median : 1.000
## Mean : 6.184 Mean : 41.12 Mean : 2.595 Mean : 2.106
## 3rd Qu.: 8.000 3rd Qu.: 64.00 3rd Qu.: 4.000 3rd Qu.: 3.000
## Max. :84.000 Max. :519.00 Max. :27.000 Max. :17.000
## NA's :19187 NA's :19187
## GIDP
## Min. : 0.000
## 1st Qu.: 1.000
## Median : 3.000
## Mean : 5.664
## 3rd Qu.: 8.000
## Max. :47.000
## NA's :20318
summary(People)
## playerID birthYear birthMonth birthDay
## Length:20370 Min. :1820 Min. : 1.00 Min. : 1.00
## Class :character 1st Qu.:1897 1st Qu.: 4.00 1st Qu.: 8.00
## Mode :character Median :1943 Median : 7.00 Median :16.00
## Mean :1935 Mean : 6.63 Mean :15.62
## 3rd Qu.:1974 3rd Qu.:10.00 3rd Qu.:23.00
## Max. :2001 Max. :12.00 Max. :31.00
## NA's :111 NA's :279 NA's :420
## birthCountry birthState birthCity deathYear
## Length:20370 Length:20370 Length:20370 Min. :1872
## Class :character Class :character Class :character 1st Qu.:1944
## Mode :character Mode :character Mode :character Median :1969
## Mean :1967
## 3rd Qu.:1994
## Max. :2022
## NA's :10366
## deathMonth deathDay deathCountry deathState
## Min. : 1.00 Min. : 1.00 Length:20370 Length:20370
## 1st Qu.: 3.00 1st Qu.: 8.00 Class :character Class :character
## Median : 6.00 Median :15.00 Mode :character Mode :character
## Mean : 6.48 Mean :15.52
## 3rd Qu.:10.00 3rd Qu.:23.00
## Max. :12.00 Max. :31.00
## NA's :10367 NA's :10368
## deathCity nameFirst nameLast nameGiven
## Length:20370 Length:20370 Length:20370 Length:20370
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## weight height bats throws debut
## Min. : 65 Min. :43.00 B : 1242 L : 3909 Length:20370
## 1st Qu.:172 1st Qu.:71.00 L : 5321 R :15483 Class :character
## Median :185 Median :72.00 R :12626 S : 1 Mode :character
## Mean :188 Mean :72.36 NA's: 1181 NA's: 977
## 3rd Qu.:200 3rd Qu.:74.00
## Max. :320 Max. :83.00
## NA's :816 NA's :736
## finalGame retroID bbrefID deathDate
## Length:20370 Length:20370 Length:20370 Min. :1872-03-17
## Class :character Class :character Class :character 1st Qu.:1944-01-31
## Mode :character Mode :character Mode :character Median :1969-04-10
## Mean :1967-07-21
## 3rd Qu.:1994-11-02
## Max. :2022-02-09
## NA's :10368
## birthDate
## Min. :1820-04-17
## 1st Qu.:1899-02-01
## Median :1944-03-22
## Mean :1936-12-02
## 3rd Qu.:1975-01-19
## Max. :2001-03-01
## NA's :420
#a.1.
Batting %>%
group_by(playerID) %>%
summarise(sum_HR = sum(HR), sum_SB = sum(SB)) %>%
filter(sum_HR >= 300 , sum_SB >=300) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, sum_HR, sum_SB)
## # A tibble: 8 × 4
## nameFirst nameLast sum_HR sum_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
#a.2.
Pitching %>%
group_by(playerID) %>%
summarise(sum_W = sum(W), sum_SO = sum(SO)) %>%
filter(sum_W >= 300 , sum_SO >=3000) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, sum_W, sum_SO)
## # A tibble: 10 × 4
## nameFirst nameLast sum_W sum_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
#b.
Batting %>%
mutate(BA=H/AB) %>%
filter(HR>=50) %>%
inner_join(People, by = c("playerID" = "playerID")) %>%
select(nameFirst, nameLast, yearID, HR, BA) %>%
arrange(BA)
## nameFirst nameLast yearID HR BA
## 1 Pete Alonso 2019 53 0.2596315
## 2 Jose Bautista 2010 54 0.2601054
## 3 Andruw Jones 2005 51 0.2627986
## 4 Roger Maris 1961 61 0.2694915
## 5 Greg Vaughn 1998 50 0.2722513
## 6 Cecil Fielder 1990 51 0.2774869
## 7 Mark McGwire 1999 65 0.2783109
## 8 Giancarlo Stanton 2017 59 0.2814070
## 9 Aaron Judge 2017 52 0.2841328
## 10 Ken Griffey 1998 56 0.2843602
## 11 Chris Davis 2013 53 0.2859589
## 12 David Ortiz 2006 54 0.2867384
## 13 Prince Fielder 2007 50 0.2879581
## 14 Sammy Sosa 1999 63 0.2880000
## 15 Brady Anderson 1996 50 0.2970639
## 16 Mark McGwire 1998 70 0.2986248
## 17 Alex Rodriguez 2002 57 0.2996795
## 18 Johnny Mize 1947 51 0.3020478
## 19 Jim Thome 2002 52 0.3041667
## 20 Ken Griffey 1997 56 0.3042763
## 21 Sammy Sosa 1998 66 0.3079316
## 22 Ralph Kiner 1949 54 0.3096539
## 23 Mark McGwire 1996 52 0.3120567
## 24 Ryan Howard 2006 58 0.3132530
## 25 Ralph Kiner 1947 51 0.3132743
## 26 Alex Rodriguez 2007 54 0.3138937
## 27 Hank Greenberg 1938 58 0.3147482
## 28 Albert Belle 1995 50 0.3168498
## 29 Mickey Mantle 1961 54 0.3171206
## 30 Willie Mays 1965 52 0.3172043
## 31 Alex Rodriguez 2001 52 0.3180380
## 32 Willie Mays 1955 51 0.3189655
## 33 Sammy Sosa 2000 50 0.3195364
## 34 George Foster 1977 52 0.3203252
## 35 Babe Ruth 1928 54 0.3227612
## 36 Luis Gonzalez 2001 57 0.3251232
## 37 Sammy Sosa 2001 64 0.3275563
## 38 Barry Bonds 2001 73 0.3277311
## 39 Jimmie Foxx 1938 50 0.3486726
## 40 Mickey Mantle 1956 52 0.3527205
## 41 Babe Ruth 1927 60 0.3555556
## 42 Hack Wilson 1930 56 0.3555556
## 43 Jimmie Foxx 1932 58 0.3641026
## 44 Babe Ruth 1920 54 0.3763676
## 45 Babe Ruth 1921 59 0.3777778