Import your 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
data_small <- data %>% 
    
    select(team_name, wins, sb_winner, year) %>%
    filter(sb_winner %in% c("Won Superbowl"))
    data_small
## # A tibble: 20 × 4
##    team_name   wins sb_winner      year
##    <chr>      <dbl> <chr>         <dbl>
##  1 Ravens        12 Won Superbowl  2000
##  2 Patriots      11 Won Superbowl  2001
##  3 Buccaneers    12 Won Superbowl  2002
##  4 Patriots      14 Won Superbowl  2003
##  5 Patriots      14 Won Superbowl  2004
##  6 Steelers      11 Won Superbowl  2005
##  7 Colts         12 Won Superbowl  2006
##  8 Giants        10 Won Superbowl  2007
##  9 Steelers      12 Won Superbowl  2008
## 10 Saints        13 Won Superbowl  2009
## 11 Packers       10 Won Superbowl  2010
## 12 Giants         9 Won Superbowl  2011
## 13 Ravens        10 Won Superbowl  2012
## 14 Seahawks      13 Won Superbowl  2013
## 15 Patriots      12 Won Superbowl  2014
## 16 Broncos       12 Won Superbowl  2015
## 17 Patriots      14 Won Superbowl  2016
## 18 Eagles        13 Won Superbowl  2017
## 19 Patriots      11 Won Superbowl  2018
## 20 Chiefs        12 Won Superbowl  2019

Pivoting

long to wide form

data_small
## # A tibble: 20 × 4
##    team_name   wins sb_winner      year
##    <chr>      <dbl> <chr>         <dbl>
##  1 Ravens        12 Won Superbowl  2000
##  2 Patriots      11 Won Superbowl  2001
##  3 Buccaneers    12 Won Superbowl  2002
##  4 Patriots      14 Won Superbowl  2003
##  5 Patriots      14 Won Superbowl  2004
##  6 Steelers      11 Won Superbowl  2005
##  7 Colts         12 Won Superbowl  2006
##  8 Giants        10 Won Superbowl  2007
##  9 Steelers      12 Won Superbowl  2008
## 10 Saints        13 Won Superbowl  2009
## 11 Packers       10 Won Superbowl  2010
## 12 Giants         9 Won Superbowl  2011
## 13 Ravens        10 Won Superbowl  2012
## 14 Seahawks      13 Won Superbowl  2013
## 15 Patriots      12 Won Superbowl  2014
## 16 Broncos       12 Won Superbowl  2015
## 17 Patriots      14 Won Superbowl  2016
## 18 Eagles        13 Won Superbowl  2017
## 19 Patriots      11 Won Superbowl  2018
## 20 Chiefs        12 Won Superbowl  2019
data_long <- data_small %>%
    
    pivot_longer(cols = c(`wins`), 
                 names_to = "Won", 
                 values_to = "Total Wins")

data_long
## # A tibble: 20 × 5
##    team_name  sb_winner      year Won   `Total Wins`
##    <chr>      <chr>         <dbl> <chr>        <dbl>
##  1 Ravens     Won Superbowl  2000 wins            12
##  2 Patriots   Won Superbowl  2001 wins            11
##  3 Buccaneers Won Superbowl  2002 wins            12
##  4 Patriots   Won Superbowl  2003 wins            14
##  5 Patriots   Won Superbowl  2004 wins            14
##  6 Steelers   Won Superbowl  2005 wins            11
##  7 Colts      Won Superbowl  2006 wins            12
##  8 Giants     Won Superbowl  2007 wins            10
##  9 Steelers   Won Superbowl  2008 wins            12
## 10 Saints     Won Superbowl  2009 wins            13
## 11 Packers    Won Superbowl  2010 wins            10
## 12 Giants     Won Superbowl  2011 wins             9
## 13 Ravens     Won Superbowl  2012 wins            10
## 14 Seahawks   Won Superbowl  2013 wins            13
## 15 Patriots   Won Superbowl  2014 wins            12
## 16 Broncos    Won Superbowl  2015 wins            12
## 17 Patriots   Won Superbowl  2016 wins            14
## 18 Eagles     Won Superbowl  2017 wins            13
## 19 Patriots   Won Superbowl  2018 wins            11
## 20 Chiefs     Won Superbowl  2019 wins            12

wide to long form

data_long %>%

pivot_wider(names_from = team_name, 
                values_from = `Total Wins`)
## # A tibble: 20 × 15
##    sb_winner      year Won   Ravens Patriots Bucca…¹ Steel…² Colts Giants Saints
##    <chr>         <dbl> <chr>  <dbl>    <dbl>   <dbl>   <dbl> <dbl>  <dbl>  <dbl>
##  1 Won Superbowl  2000 wins      12       NA      NA      NA    NA     NA     NA
##  2 Won Superbowl  2001 wins      NA       11      NA      NA    NA     NA     NA
##  3 Won Superbowl  2002 wins      NA       NA      12      NA    NA     NA     NA
##  4 Won Superbowl  2003 wins      NA       14      NA      NA    NA     NA     NA
##  5 Won Superbowl  2004 wins      NA       14      NA      NA    NA     NA     NA
##  6 Won Superbowl  2005 wins      NA       NA      NA      11    NA     NA     NA
##  7 Won Superbowl  2006 wins      NA       NA      NA      NA    12     NA     NA
##  8 Won Superbowl  2007 wins      NA       NA      NA      NA    NA     10     NA
##  9 Won Superbowl  2008 wins      NA       NA      NA      12    NA     NA     NA
## 10 Won Superbowl  2009 wins      NA       NA      NA      NA    NA     NA     13
## 11 Won Superbowl  2010 wins      NA       NA      NA      NA    NA     NA     NA
## 12 Won Superbowl  2011 wins      NA       NA      NA      NA    NA      9     NA
## 13 Won Superbowl  2012 wins      10       NA      NA      NA    NA     NA     NA
## 14 Won Superbowl  2013 wins      NA       NA      NA      NA    NA     NA     NA
## 15 Won Superbowl  2014 wins      NA       12      NA      NA    NA     NA     NA
## 16 Won Superbowl  2015 wins      NA       NA      NA      NA    NA     NA     NA
## 17 Won Superbowl  2016 wins      NA       14      NA      NA    NA     NA     NA
## 18 Won Superbowl  2017 wins      NA       NA      NA      NA    NA     NA     NA
## 19 Won Superbowl  2018 wins      NA       11      NA      NA    NA     NA     NA
## 20 Won Superbowl  2019 wins      NA       NA      NA      NA    NA     NA     NA
## # … with 5 more variables: Packers <dbl>, Seahawks <dbl>, Broncos <dbl>,
## #   Eagles <dbl>, Chiefs <dbl>, and abbreviated variable names ¹​Buccaneers,
## #   ²​Steelers

Separating and Uniting

Separate a column

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
data_sep <- data %>%
    
    separate(col = playoffs, into = c("Playoffs", "No Playoffs"))
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 240 rows [1, 2, 6, 7, 12,
## 13, 17, 18, 22, 23, 27, 28, 32, 33, 34, 37, 38, 43, 48, 53, ...].

Unite two columns

data_united <- data %>%
    unite(col = "teamname", team:team_name, sep = " ")
    data_united
## # A tibble: 638 × 14
##    teamname     year  wins  loss point…¹ point…² point…³ margi…⁴ stren…⁵ simpl…⁶
##    <chr>       <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 Miami Dolp…  2000    11     5     323     226      97     6.1     1       7.1
##  2 Indianapol…  2000    10     6     429     326     103     6.4     1.5     7.9
##  3 New York J…  2000     9     7     321     321       0     0       3.5     3.5
##  4 Buffalo Bi…  2000     8     8     315     350     -35    -2.2     2.2     0  
##  5 New Englan…  2000     5    11     276     338     -62    -3.9     1.4    -2.5
##  6 Tennessee …  2000    13     3     346     191     155     9.7    -1.3     8.3
##  7 Baltimore …  2000    12     4     333     165     168    10.5    -2.5     8  
##  8 Pittsburgh…  2000     9     7     321     255      66     4.1    -0.2     3.9
##  9 Jacksonvil…  2000     7     9     367     327      40     2.5    -1.4     1.1
## 10 Cincinnati…  2000     4    12     185     359    -174   -10.9     0.4   -10.5
## # … with 628 more rows, 4 more variables: offensive_ranking <dbl>,
## #   defensive_ranking <dbl>, playoffs <chr>, sb_winner <chr>, and abbreviated
## #   variable names ¹​points_for, ²​points_against, ³​points_differential,
## #   ⁴​margin_of_victory, ⁵​strength_of_schedule, ⁶​simple_rating

Missing Values

data <- tibble(
  year   = c(2004, 2005, 2006, 2007, 2008),
  wins    = c(   11,    12,    13,    14,    15),
  loss = c(5, 4, 3, 2, 1))
data %>%
    pivot_wider(names_from = year, values_from = wins)
## # A tibble: 5 × 6
##    loss `2004` `2005` `2006` `2007` `2008`
##   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
## 1     5     11     NA     NA     NA     NA
## 2     4     NA     12     NA     NA     NA
## 3     3     NA     NA     13     NA     NA
## 4     2     NA     NA     NA     14     NA
## 5     1     NA     NA     NA     NA     15