Import data

# excel file
data <- read_excel("../00_data/MyData.xlsx")
data
## # A tibble: 638 × 15
##    team        team_…¹  year  wins  loss point…² point…³ point…⁴ margi…⁵ stren…⁶
##    <chr>       <chr>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 Miami       Dolphi…  2000    11     5     323     226      97     6.1     1  
##  2 Indianapol… Colts    2000    10     6     429     326     103     6.4     1.5
##  3 New York    Jets     2000     9     7     321     321       0     0       3.5
##  4 Buffalo     Bills    2000     8     8     315     350     -35    -2.2     2.2
##  5 New England Patrio…  2000     5    11     276     338     -62    -3.9     1.4
##  6 Tennessee   Titans   2000    13     3     346     191     155     9.7    -1.3
##  7 Baltimore   Ravens   2000    12     4     333     165     168    10.5    -2.5
##  8 Pittsburgh  Steele…  2000     9     7     321     255      66     4.1    -0.2
##  9 Jacksonvil… Jaguars  2000     7     9     367     327      40     2.5    -1.4
## 10 Cincinnati  Bengals  2000     4    12     185     359    -174   -10.9     0.4
## # … with 628 more rows, 5 more variables: simple_rating <dbl>,
## #   offensive_ranking <dbl>, defensive_ranking <dbl>, playoffs <chr>,
## #   sb_winner <chr>, and abbreviated variable names ¹​team_name, ²​points_for,
## #   ³​points_against, ⁴​points_differential, ⁵​margin_of_victory,
## #   ⁶​strength_of_schedule

Filter rows

filter(data, sb_winner == "Won Superbowl")
## # A tibble: 20 × 15
##    team        team_…¹  year  wins  loss point…² point…³ point…⁴ margi…⁵ stren…⁶
##    <chr>       <chr>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 Baltimore   Ravens   2000    12     4     333     165     168    10.5    -2.5
##  2 New England Patrio…  2001    11     5     371     272      99     6.2    -1.9
##  3 Tampa Bay   Buccan…  2002    12     4     346     196     150     9.4    -0.6
##  4 New England Patrio…  2003    14     2     348     238     110     6.9     0.1
##  5 New England Patrio…  2004    14     2     437     260     177    11.1     1.8
##  6 Pittsburgh  Steele…  2005    11     5     389     258     131     8.2    -0.4
##  7 Indianapol… Colts    2006    12     4     427     360      67     4.2     1.7
##  8 New York    Giants   2007    10     6     373     351      22     1.4     1.9
##  9 Pittsburgh  Steele…  2008    12     4     347     223     124     7.8     2  
## 10 New Orleans Saints   2009    13     3     510     341     169    10.6     0.2
## 11 Green Bay   Packers  2010    10     6     388     240     148     9.3     1.7
## 12 New York    Giants   2011     9     7     394     400      -6    -0.4     2  
## 13 Baltimore   Ravens   2012    10     6     398     344      54     3.4    -0.5
## 14 Seattle     Seahaw…  2013    13     3     417     231     186    11.6     1.4
## 15 New England Patrio…  2014    12     4     468     313     155     9.7     1.3
## 16 Denver      Broncos  2015    12     4     355     296      59     3.7     2.1
## 17 New England Patrio…  2016    14     2     441     250     191    11.9    -2.7
## 18 Philadelph… Eagles   2017    13     3     457     295     162    10.1    -0.7
## 19 New England Patrio…  2018    11     5     436     325     111     6.9    -1.8
## 20 Kansas City Chiefs   2019    12     4     451     308     143     8.9     0.2
## # … with 5 more variables: simple_rating <dbl>, offensive_ranking <dbl>,
## #   defensive_ranking <dbl>, playoffs <chr>, sb_winner <chr>, and abbreviated
## #   variable names ¹​team_name, ²​points_for, ³​points_against,
## #   ⁴​points_differential, ⁵​margin_of_victory, ⁶​strength_of_schedule

Arrange rows

arrange(data, desc (wins))
## # A tibble: 638 × 15
##    team        team_…¹  year  wins  loss point…² point…³ point…⁴ margi…⁵ stren…⁶
##    <chr>       <chr>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 New England Patrio…  2007    16     0     589     274     315    19.7     0.4
##  2 Pittsburgh  Steele…  2004    15     1     372     251     121     7.6     1.4
##  3 Green Bay   Packers  2011    15     1     560     359     201    12.6    -1.2
##  4 Carolina    Panthe…  2015    15     1     500     308     192    12      -3.9
##  5 St. Louis   Rams     2001    14     2     503     273     230    14.4    -1  
##  6 New England Patrio…  2003    14     2     348     238     110     6.9     0.1
##  7 New England Patrio…  2004    14     2     437     260     177    11.1     1.8
##  8 Indianapol… Colts    2005    14     2     439     247     192    12      -1.2
##  9 San Diego   Charge…  2006    14     2     492     303     189    11.8    -1.6
## 10 Indianapol… Colts    2009    14     2     416     307     109     6.8    -0.9
## # … with 628 more rows, 5 more variables: simple_rating <dbl>,
## #   offensive_ranking <dbl>, defensive_ranking <dbl>, playoffs <chr>,
## #   sb_winner <chr>, and abbreviated variable names ¹​team_name, ²​points_for,
## #   ³​points_against, ⁴​points_differential, ⁵​margin_of_victory,
## #   ⁶​strength_of_schedule

Select columns

select(data, sb_winner, wins, year)
## # A tibble: 638 × 3
##    sb_winner      wins  year
##    <chr>         <dbl> <dbl>
##  1 No Superbowl     11  2000
##  2 No Superbowl     10  2000
##  3 No Superbowl      9  2000
##  4 No Superbowl      8  2000
##  5 No Superbowl      5  2000
##  6 No Superbowl     13  2000
##  7 Won Superbowl    12  2000
##  8 No Superbowl      9  2000
##  9 No Superbowl      7  2000
## 10 No Superbowl      4  2000
## # … with 628 more rows
select(data, sb_winner, wins, year, playoffs)
## # A tibble: 638 × 4
##    sb_winner      wins  year playoffs   
##    <chr>         <dbl> <dbl> <chr>      
##  1 No Superbowl     11  2000 Playoffs   
##  2 No Superbowl     10  2000 Playoffs   
##  3 No Superbowl      9  2000 No Playoffs
##  4 No Superbowl      8  2000 No Playoffs
##  5 No Superbowl      5  2000 No Playoffs
##  6 No Superbowl     13  2000 Playoffs   
##  7 Won Superbowl    12  2000 Playoffs   
##  8 No Superbowl      9  2000 No Playoffs
##  9 No Superbowl      7  2000 No Playoffs
## 10 No Superbowl      4  2000 No Playoffs
## # … with 628 more rows

Add columns

mutate(data,
       record_differential = wins - loss)
## # A tibble: 638 × 16
##    team        team_…¹  year  wins  loss point…² point…³ point…⁴ margi…⁵ stren…⁶
##    <chr>       <chr>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 Miami       Dolphi…  2000    11     5     323     226      97     6.1     1  
##  2 Indianapol… Colts    2000    10     6     429     326     103     6.4     1.5
##  3 New York    Jets     2000     9     7     321     321       0     0       3.5
##  4 Buffalo     Bills    2000     8     8     315     350     -35    -2.2     2.2
##  5 New England Patrio…  2000     5    11     276     338     -62    -3.9     1.4
##  6 Tennessee   Titans   2000    13     3     346     191     155     9.7    -1.3
##  7 Baltimore   Ravens   2000    12     4     333     165     168    10.5    -2.5
##  8 Pittsburgh  Steele…  2000     9     7     321     255      66     4.1    -0.2
##  9 Jacksonvil… Jaguars  2000     7     9     367     327      40     2.5    -1.4
## 10 Cincinnati  Bengals  2000     4    12     185     359    -174   -10.9     0.4
## # … with 628 more rows, 6 more variables: simple_rating <dbl>,
## #   offensive_ranking <dbl>, defensive_ranking <dbl>, playoffs <chr>,
## #   sb_winner <chr>, record_differential <dbl>, and abbreviated variable names
## #   ¹​team_name, ²​points_for, ³​points_against, ⁴​points_differential,
## #   ⁵​margin_of_victory, ⁶​strength_of_schedule

Summarize by groups

data
## # A tibble: 638 × 15
##    team        team_…¹  year  wins  loss point…² point…³ point…⁴ margi…⁵ stren…⁶
##    <chr>       <chr>   <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 Miami       Dolphi…  2000    11     5     323     226      97     6.1     1  
##  2 Indianapol… Colts    2000    10     6     429     326     103     6.4     1.5
##  3 New York    Jets     2000     9     7     321     321       0     0       3.5
##  4 Buffalo     Bills    2000     8     8     315     350     -35    -2.2     2.2
##  5 New England Patrio…  2000     5    11     276     338     -62    -3.9     1.4
##  6 Tennessee   Titans   2000    13     3     346     191     155     9.7    -1.3
##  7 Baltimore   Ravens   2000    12     4     333     165     168    10.5    -2.5
##  8 Pittsburgh  Steele…  2000     9     7     321     255      66     4.1    -0.2
##  9 Jacksonvil… Jaguars  2000     7     9     367     327      40     2.5    -1.4
## 10 Cincinnati  Bengals  2000     4    12     185     359    -174   -10.9     0.4
## # … with 628 more rows, 5 more variables: simple_rating <dbl>,
## #   offensive_ranking <dbl>, defensive_ranking <dbl>, playoffs <chr>,
## #   sb_winner <chr>, and abbreviated variable names ¹​team_name, ²​points_for,
## #   ³​points_against, ⁴​points_differential, ⁵​margin_of_victory,
## #   ⁶​strength_of_schedule
# average wins
summarise(data, mean(wins))
## # A tibble: 1 × 1
##   `mean(wins)`
##          <dbl>
## 1         7.98
data %>%
    
    group_by(team_name) %>%
    
    summarise(data, mean(wins))
## # A tibble: 20,416 × 16
## # Groups:   team_name [32]
##    team_name team       year  wins  loss point…¹ point…² point…³ margi…⁴ stren…⁵
##    <chr>     <chr>     <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 Dolphins  Miami      2000    11     5     323     226      97     6.1     1  
##  2 Colts     Indianap…  2000    10     6     429     326     103     6.4     1.5
##  3 Jets      New York   2000     9     7     321     321       0     0       3.5
##  4 Bills     Buffalo    2000     8     8     315     350     -35    -2.2     2.2
##  5 Patriots  New Engl…  2000     5    11     276     338     -62    -3.9     1.4
##  6 Titans    Tennessee  2000    13     3     346     191     155     9.7    -1.3
##  7 Ravens    Baltimore  2000    12     4     333     165     168    10.5    -2.5
##  8 Steelers  Pittsbur…  2000     9     7     321     255      66     4.1    -0.2
##  9 Jaguars   Jacksonv…  2000     7     9     367     327      40     2.5    -1.4
## 10 Bengals   Cincinna…  2000     4    12     185     359    -174   -10.9     0.4
## # … with 20,406 more rows, 6 more variables: simple_rating <dbl>,
## #   offensive_ranking <dbl>, defensive_ranking <dbl>, playoffs <chr>,
## #   sb_winner <chr>, `mean(wins)` <dbl>, and abbreviated variable names
## #   ¹​points_for, ²​points_against, ³​points_differential, ⁴​margin_of_victory,
## #   ⁵​strength_of_schedule