data(cars)
median(cars$speed)
## [1] 15
C. 15
library(robotstxt)
library(rvest)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
url <- "https://en.wikipedia.org/wiki/Player_efficiency_rating"
paths_allowed(url)
## Warning: package 'future' was built under R version 4.4.3
## en.wikipedia.org
##
## [1] TRUE
webpage <- read_html(url)
tables <- html_elements(webpage, "table")
per_table <- html_table(tables[3])
per_leaders <- per_table[[1]]
names(per_leaders) <- c("Rank", "Player", "Pos", "Teams", "PER")
head(per_leaders)
## # A tibble: 6 × 5
## Rank Player Pos Teams PER
## <int> <chr> <chr> <chr> <dbl>
## 1 1 Nikola Jokić^ C "Denver Nuggets (2015–present)" 28.8
## 2 2 Joel Embiid^ C "Philadelphia 76ers (2014–present)" 28.0
## 3 3 Michael Jordan* SG "Chicago Bulls (1984–1993, 1995–1998)Wash… 27.9
## 4 4 LeBron James^ SF "Cleveland Cavaliers (2003–2010, 2014–201… 26.7
## 5 5 Anthony Davis^ PF/C "New Orleans Hornets/Pelicans (2012–2019)… 26.7
## 6 6 Shaquille O'Neal* C "Orlando Magic (1992–1996)Los Angeles Lak… 26.4
True
MLB Winning Formula: How Hitting and Pitching Relate to Team Success
batting_raw <- read.csv("mlb_batting_2026.csv")
pitching_raw <- read.csv("mlb_pitching_2026.csv")
head(batting_raw)
## Tm X.Bat BatAge R.G G PA AB R H X2B X3B HR RBI
## 1 Arizona Diamondbacks 56 28.5 4.56 162 6148 5451 739 1341 269 42 166 706
## 2 Athletics 60 26.7 4.31 162 6126 5488 699 1345 244 17 194 670
## 3 Atlanta Braves 62 29.5 4.58 162 6053 5471 742 1348 251 13 204 711
## 4 Baltimore Orioles 60 26.6 4.46 161 6079 5373 718 1256 235 20 199 691
## 5 Boston Red Sox 56 28.3 4.25 162 6112 5414 689 1327 285 26 163 658
## 6 Chicago Cubs 62 28.8 5.25 162 6382 5564 850 1398 274 26 220 804
## SB CS BB SO BA OBP SLG OPS OPS. TB GDP HBP SH SF IBB LOB
## 1 103 27 541 1195 0.246 0.318 0.402 0.720 100 2192 103 58 41 54 16 1114
## 2 91 32 520 1373 0.245 0.317 0.402 0.719 97 2205 130 70 21 25 18 1110
## 3 83 23 465 1310 0.246 0.309 0.409 0.718 97 2237 89 56 11 47 13 1050
## 4 78 33 586 1518 0.234 0.314 0.396 0.710 101 2128 102 62 18 38 19 1083
## 5 107 39 517 1326 0.245 0.319 0.398 0.717 102 2153 98 95 36 46 13 1135
## 6 114 22 674 1345 0.251 0.339 0.428 0.767 111 2384 110 87 10 46 25 1233
head(pitching_raw)
## Tm X.P PAge RA.G W L W.L. ERA G GS GF CG tSho cSho
## 1 Arizona Diamondbacks 34 30.4 4.51 86 76 0.531 4.18 162 162 161 1 10 0
## 2 Athletics 35 28.6 5.78 64 98 0.395 5.51 162 162 161 1 7 0
## 3 Atlanta Braves 36 30.7 3.86 94 68 0.580 3.63 162 162 161 1 10 1
## 4 Baltimore Orioles 32 29.8 4.59 79 82 0.491 4.07 161 161 160 1 2 0
## 5 Boston Red Sox 32 29.6 3.77 87 75 0.537 3.54 162 162 162 0 18 0
## 6 Chicago Cubs 40 31.6 4.34 89 73 0.549 4.12 162 162 162 0 6 0
## SV IP H R ER HR BB IBB SO HBP BK WP BF ERA. FIP WHIP H9
## 1 38 1442.0 1368 730 669 186 483 21 1140 67 7 31 6110 99 4.34 1.284 8.5
## 2 39 1430.0 1523 937 875 247 637 23 1356 75 8 51 6390 81 4.94 1.510 9.6
## 3 49 1438.0 1259 626 580 185 529 15 1350 61 5 30 6037 117 4.13 1.243 7.9
## 4 46 1436.1 1385 739 649 169 535 17 1305 50 4 45 6152 101 4.03 1.337 8.7
## 5 46 1440.0 1307 611 566 153 469 11 1398 66 4 35 6028 116 3.65 1.233 8.2
## 6 36 1447.0 1323 703 662 231 500 13 1260 62 4 35 6062 103 4.60 1.260 8.2
## HR9 BB9 SO9 SO.W LOB
## 1 1.2 3.0 7.1 2.36 1076
## 2 1.6 4.0 8.5 2.13 1180
## 3 1.2 3.3 8.4 2.55 1114
## 4 1.1 3.4 8.2 2.44 1131
## 5 1.0 2.9 8.7 2.98 1113
## 6 1.4 3.1 7.8 2.52 1042
names(batting_raw)
## [1] "Tm" "X.Bat" "BatAge" "R.G" "G" "PA" "AB" "R"
## [9] "H" "X2B" "X3B" "HR" "RBI" "SB" "CS" "BB"
## [17] "SO" "BA" "OBP" "SLG" "OPS" "OPS." "TB" "GDP"
## [25] "HBP" "SH" "SF" "IBB" "LOB"
names(pitching_raw)
## [1] "Tm" "X.P" "PAge" "RA.G" "W" "L" "W.L." "ERA" "G" "GS"
## [11] "GF" "CG" "tSho" "cSho" "SV" "IP" "H" "R" "ER" "HR"
## [21] "BB" "IBB" "SO" "HBP" "BK" "WP" "BF" "ERA." "FIP" "WHIP"
## [31] "H9" "HR9" "BB9" "SO9" "SO.W" "LOB"
Description
ERA (Earned Run Average): Average number of earned runs a team’s pitchers allow per nine innings pitched. A lower ERA indicates better pitching performance.
OPS (On-Base Plus Slugging): Sum of a team’s on-base percentage, measuring the ability to reach base, and slugging percentage, measuring hitting power. A higher OPS indicates better hitting performance.
Keep Team Rows and Relevant Columns
batting <- batting_raw[1:30, c("Tm", "G", "HR", "OPS")]
pitching <- pitching_raw[1:30, c("Tm", "W", "L", "ERA")]
names(batting) <- c("Team", "Games", "Home_Runs", "OPS")
names(pitching) <- c("Team", "Wins", "Losses", "ERA")
head(batting)
## Team Games Home_Runs OPS
## 1 Arizona Diamondbacks 162 166 0.720
## 2 Athletics 162 194 0.719
## 3 Atlanta Braves 162 204 0.718
## 4 Baltimore Orioles 161 199 0.710
## 5 Boston Red Sox 162 163 0.717
## 6 Chicago Cubs 162 220 0.767
head(pitching)
## Team Wins Losses ERA
## 1 Arizona Diamondbacks 86 76 4.18
## 2 Athletics 64 98 5.51
## 3 Atlanta Braves 94 68 3.63
## 4 Baltimore Orioles 79 82 4.07
## 5 Boston Red Sox 87 75 3.54
## 6 Chicago Cubs 89 73 4.12
Organizing Teams
batting <- arrange(batting, Team)
pitching <- arrange(pitching, Team)
batting$Team == pitching$Team
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Combine into Single Dataset
mlb <- batting
mlb$Wins <- pitching$Wins
mlb$Losses <- pitching$Losses
mlb$ERA <- pitching$ERA
head(mlb)
## Team Games Home_Runs OPS Wins Losses ERA
## 1 Arizona Diamondbacks 162 166 0.720 86 76 4.18
## 2 Athletics 162 194 0.719 64 98 5.51
## 3 Atlanta Braves 162 204 0.718 94 68 3.63
## 4 Baltimore Orioles 161 199 0.710 79 82 4.07
## 5 Boston Red Sox 162 163 0.717 87 75 3.54
## 6 Chicago Cubs 162 220 0.767 89 73 4.12
Calculate Needed Stats
mlb$Season <- 2026
mlb$Winning_Percentage <- mlb$Wins / (mlb$Wins + mlb$Losses)
mlb$Home_Runs_Per_Game <- mlb$Home_Runs / mlb$Games
mlb <- arrange(mlb, desc(Winning_Percentage))
head(mlb)
## Team Games Home_Runs OPS Wins Losses ERA Season
## 1 Milwaukee Brewers 162 154 0.743 103 59 3.51 2026
## 2 Los Angeles Dodgers 162 204 0.762 100 62 3.55 2026
## 3 Tampa Bay Rays 162 169 0.728 98 64 3.70 2026
## 4 Atlanta Braves 162 204 0.718 94 68 3.63 2026
## 5 New York Yankees 161 223 0.720 93 68 3.23 2026
## 6 San Diego Padres 162 183 0.718 91 71 3.99 2026
## Winning_Percentage Home_Runs_Per_Game
## 1 0.6358025 0.9506173
## 2 0.6172840 1.2592593
## 3 0.6049383 1.0432099
## 4 0.5802469 1.2592593
## 5 0.5776398 1.3850932
## 6 0.5617284 1.1296296
Check
dim(mlb)
## [1] 30 10
str(mlb)
## 'data.frame': 30 obs. of 10 variables:
## $ Team : chr "Milwaukee Brewers" "Los Angeles Dodgers" "Tampa Bay Rays" "Atlanta Braves" ...
## $ Games : int 162 162 162 162 161 162 162 162 162 162 ...
## $ Home_Runs : int 154 204 169 204 223 183 220 182 163 166 ...
## $ OPS : num 0.743 0.762 0.728 0.718 0.72 0.718 0.767 0.707 0.717 0.72 ...
## $ Wins : int 103 100 98 94 93 91 89 88 87 86 ...
## $ Losses : int 59 62 64 68 68 71 73 74 75 76 ...
## $ ERA : num 3.51 3.55 3.7 3.63 3.23 3.99 4.12 4.04 3.54 4.18 ...
## $ Season : num 2026 2026 2026 2026 2026 ...
## $ Winning_Percentage: num 0.636 0.617 0.605 0.58 0.578 ...
## $ Home_Runs_Per_Game: num 0.951 1.259 1.043 1.259 1.385 ...
summary(mlb)
## Team Games Home_Runs OPS
## Length:30 Min. :161.0 Min. :152.0 Min. :0.6790
## Class :character 1st Qu.:162.0 1st Qu.:168.2 1st Qu.:0.7063
## Mode :character Median :162.0 Median :182.5 Median :0.7190
## Mean :161.9 Mean :185.8 Mean :0.7188
## 3rd Qu.:162.0 3rd Qu.:204.0 3rd Qu.:0.7278
## Max. :162.0 Max. :226.0 Max. :0.7670
## Wins Losses ERA Season
## Min. : 58.00 Min. : 59.00 Min. :3.230 Min. :2026
## 1st Qu.: 76.00 1st Qu.: 74.25 1st Qu.:3.803 1st Qu.:2026
## Median : 80.00 Median : 82.00 Median :4.130 Median :2026
## Mean : 80.97 Mean : 80.97 Mean :4.173 Mean :2026
## 3rd Qu.: 87.75 3rd Qu.: 86.00 3rd Qu.:4.327 3rd Qu.:2026
## Max. :103.00 Max. :104.00 Max. :5.590 Max. :2026
## Winning_Percentage Home_Runs_Per_Game
## Min. :0.3580 Min. :0.9383
## 1st Qu.:0.4691 1st Qu.:1.0386
## Median :0.4938 Median :1.1265
## Mean :0.5000 Mean :1.1477
## 3rd Qu.:0.5417 3rd Qu.:1.2593
## Max. :0.6358 Max. :1.3951
sum(is.na(mlb))
## [1] 0
n_distinct(mlb$Team)
## [1] 30