data(cars)

Question 1

median(cars$speed)
## [1] 15

Question 2

library(jsonlite)

url <- "https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=100"

btc_data <- fromJSON(url)

btc_df <- btc_data$Data$Data 

max_close <- max(btc_df$close, na.rm = TRUE)

max_close
## [1] 124723

Question 3 (Mini Project)

Title: Comparing NFL NFC 2020 Pro Football Reference Team Statistics

Research Qs:

1. Which NFC teams had the highest win percentages in 2020?

2. Is there a relationship between points scored per game and total wins?

3. Which NFC teams allowed the fewest points per game?

Data Extraction

pacman::p_load(robotstxt, rvest)


nfl_html <- read_html("https://www.pro-football-reference.com/years/2020/")
nfl_html
## {html_document}
## <html data-version="klecko-" data-root="/home/pfr/build" lang="en" class="no-js">
## [1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8 ...
## [2] <body class="pfr">\n<div id="wrap">\n  <div id="header" role="banner">\n  ...
table_html <- html_elements(nfl_html, "table")
table_html
## {xml_nodeset (2)}
## [1] <table class="sortable stats_table" id="AFC" data-cols-to-freeze=",1">\n< ...
## [2] <table class="sortable stats_table" id="NFC" data-cols-to-freeze=",1">\n< ...
#Table for NFC Teams
tibble_list <- html_table(table_html[2], fill = TRUE)
tibble_list
## [[1]]
## # A tibble: 20 × 13
##    Tm         W     L     T     `W-L%` PF    PA    PD    MoV   SoS   SRS   OSRS 
##    <chr>      <chr> <chr> <chr> <chr>  <chr> <chr> <chr> <chr> <chr> <chr> <chr>
##  1 NFC East   NFC … NFC … NFC … NFC E… NFC … NFC … NFC … NFC … NFC … NFC … NFC …
##  2 Washingto… 7     9     0     .438   335   329   6     0.4   -1.2  -0.8  -4.1 
##  3 New York … 6     10    0     .375   280   357   -77   -4.8  0.4   -4.4  -6.7 
##  4 Dallas Co… 6     10    0     .375   395   473   -78   -4.9  -0.2  -5.1  1.0  
##  5 Philadelp… 4     11    1     .281   334   418   -84   -5.3  0.8   -4.4  -2.8 
##  6 NFC North  NFC … NFC … NFC … NFC N… NFC … NFC … NFC … NFC … NFC … NFC … NFC …
##  7 Green Bay… 13    3     0     .813   509   369   140   8.8   -1.1  7.7   5.9  
##  8 Chicago B… 8     8     0     .500   372   370   2     0.1   0.1   0.2   -2.2 
##  9 Minnesota… 7     9     0     .438   430   475   -45   -2.8  0.4   -2.4  1.3  
## 10 Detroit L… 5     11    0     .313   377   519   -142  -8.9  1.2   -7.7  -1.0 
## 11 NFC South  NFC … NFC … NFC … NFC S… NFC … NFC … NFC … NFC … NFC … NFC … NFC …
## 12 New Orlea… 12    4     0     .750   482   337   145   9.1   0.5   9.6   5.1  
## 13 Tampa Bay… 11    5     0     .688   492   355   137   8.6   0.8   9.4   6.5  
## 14 Carolina … 5     11    0     .313   350   402   -52   -3.3  2.2   -1.1  -2.4 
## 15 Atlanta F… 4     12    0     .250   396   414   -18   -1.1  1.9   0.7   -0.1 
## 16 NFC West   NFC … NFC … NFC … NFC W… NFC … NFC … NFC … NFC … NFC … NFC … NFC …
## 17 Seattle S… 12    4     0     .750   459   371   88    5.5   0.0   5.5   4.8  
## 18 Los Angel… 10    6     0     .625   372   296   76    4.8   0.7   5.4   -0.8 
## 19 Arizona C… 8     8     0     .500   410   367   43    2.7   -0.1  2.6   1.5  
## 20 San Franc… 6     10    0     .375   376   390   -14   -0.9  1.7   0.8   0.2  
## # ℹ 1 more variable: DSRS <chr>
team_stats <- tibble_list[[1]]
head(team_stats)
## # A tibble: 6 × 13
##   Tm    W     L     T     `W-L%` PF    PA    PD    MoV   SoS   SRS   OSRS  DSRS 
##   <chr> <chr> <chr> <chr> <chr>  <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 NFC … NFC … NFC … NFC … NFC E… NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC …
## 2 Wash… 7     9     0     .438   335   329   6     0.4   -1.2  -0.8  -4.1  3.2  
## 3 New … 6     10    0     .375   280   357   -77   -4.8  0.4   -4.4  -6.7  2.3  
## 4 Dall… 6     10    0     .375   395   473   -78   -4.9  -0.2  -5.1  1.0   -6.1 
## 5 Phil… 4     11    1     .281   334   418   -84   -5.3  0.8   -4.4  -2.8  -1.6 
## 6 NFC … NFC … NFC … NFC … NFC N… NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC …

Data Cleaning

str(team_stats)
## tibble [20 × 13] (S3: tbl_df/tbl/data.frame)
##  $ Tm  : chr [1:20] "NFC East" "Washington Football Team*" "New York Giants" "Dallas Cowboys" ...
##  $ W   : chr [1:20] "NFC East" "7" "6" "6" ...
##  $ L   : chr [1:20] "NFC East" "9" "10" "10" ...
##  $ T   : chr [1:20] "NFC East" "0" "0" "0" ...
##  $ W-L%: chr [1:20] "NFC East" ".438" ".375" ".375" ...
##  $ PF  : chr [1:20] "NFC East" "335" "280" "395" ...
##  $ PA  : chr [1:20] "NFC East" "329" "357" "473" ...
##  $ PD  : chr [1:20] "NFC East" "6" "-77" "-78" ...
##  $ MoV : chr [1:20] "NFC East" "0.4" "-4.8" "-4.9" ...
##  $ SoS : chr [1:20] "NFC East" "-1.2" "0.4" "-0.2" ...
##  $ SRS : chr [1:20] "NFC East" "-0.8" "-4.4" "-5.1" ...
##  $ OSRS: chr [1:20] "NFC East" "-4.1" "-6.7" "1.0" ...
##  $ DSRS: chr [1:20] "NFC East" "3.2" "2.3" "-6.1" ...
names(team_stats) <- make.names(names(team_stats))
head(team_stats)
## # A tibble: 6 × 13
##   Tm     W     L     T     W.L.  PF    PA    PD    MoV   SoS   SRS   OSRS  DSRS 
##   <chr>  <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 NFC E… NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC …
## 2 Washi… 7     9     0     .438  335   329   6     0.4   -1.2  -0.8  -4.1  3.2  
## 3 New Y… 6     10    0     .375  280   357   -77   -4.8  0.4   -4.4  -6.7  2.3  
## 4 Dalla… 6     10    0     .375  395   473   -78   -4.9  -0.2  -5.1  1.0   -6.1 
## 5 Phila… 4     11    1     .281  334   418   -84   -5.3  0.8   -4.4  -2.8  -1.6 
## 6 NFC N… NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC … NFC …
team_stats$W <- as.numeric(team_stats$W)
## Warning: NAs introduced by coercion
team_stats_clean <- team_stats[!is.na(team_stats$W), ]
head(team_stats_clean)
## # A tibble: 6 × 13
##   Tm         W L     T     W.L.  PF    PA    PD    MoV   SoS   SRS   OSRS  DSRS 
##   <chr>  <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Washi…     7 9     0     .438  335   329   6     0.4   -1.2  -0.8  -4.1  3.2  
## 2 New Y…     6 10    0     .375  280   357   -77   -4.8  0.4   -4.4  -6.7  2.3  
## 3 Dalla…     6 10    0     .375  395   473   -78   -4.9  -0.2  -5.1  1.0   -6.1 
## 4 Phila…     4 11    1     .281  334   418   -84   -5.3  0.8   -4.4  -2.8  -1.6 
## 5 Green…    13 3     0     .813  509   369   140   8.8   -1.1  7.7   5.9   1.8  
## 6 Chica…     8 8     0     .500  372   370   2     0.1   0.1   0.2   -2.2  2.4

Research Qs

1. Which NFC teams had the highest win percentages in 2020?

top_win_pct <- team_stats_clean[order(team_stats_clean$W.L., decreasing = TRUE), ]
head(top_win_pct)
## # A tibble: 6 × 13
##   Tm         W L     T     W.L.  PF    PA    PD    MoV   SoS   SRS   OSRS  DSRS 
##   <chr>  <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Green…    13 3     0     .813  509   369   140   8.8   -1.1  7.7   5.9   1.8  
## 2 New O…    12 4     0     .750  482   337   145   9.1   0.5   9.6   5.1   4.5  
## 3 Seatt…    12 4     0     .750  459   371   88    5.5   0.0   5.5   4.8   0.7  
## 4 Tampa…    11 5     0     .688  492   355   137   8.6   0.8   9.4   6.5   2.8  
## 5 Los A…    10 6     0     .625  372   296   76    4.8   0.7   5.4   -0.8  6.2  
## 6 Chica…     8 8     0     .500  372   370   2     0.1   0.1   0.2   -2.2  2.4

2. Is there a relationship between points scored per game and total wins?

team_stats_clean$PF <- as.numeric(team_stats_clean$PF)

plot(team_stats_clean$PF, team_stats_clean$W,
     xlab = "Points Scored (PF)",
     ylab = "Wins",
     main = "Points Scored vs Wins",
     pch = 19, col = "darkgreen")

abline(lm(W ~ PF, data = team_stats_clean), col = "red")

cor(team_stats_clean$PF, team_stats_clean$W)
## [1] 0.7642514

Yes there is a strong postive correlation and relationship between ppg and total wins

3. Which NFC teams allowed the fewest points per game?

team_stats_clean$PA <- as.numeric(team_stats_clean$PA)
best_defense <- team_stats_clean[order(team_stats_clean$PA), ]
best_defense[, c("Tm", "PA", "W", "L")][1:5, ]
## # A tibble: 5 × 4
##   Tm                           PA     W L    
##   <chr>                     <dbl> <dbl> <chr>
## 1 Los Angeles Rams+           296    10 6    
## 2 Washington Football Team*   329     7 9    
## 3 New Orleans Saints*         337    12 4    
## 4 Tampa Bay Buccaneers+       355    11 5    
## 5 New York Giants             357     6 10