Skip to contents

This function converts dataframes with "home_" and "away_" prefixed columns to "team_" and "opponent_", and doubles the rows. This makes sure that there's one row for each team (as opposed to one row for each game).

Usage

clean_homeaway(dataframe, invert = NULL)

Arguments

dataframe

dataframe

invert

a character vector of columns that gets inverted when referring to the away team (e.g. home spread = 1 gets converted to away_spread = -1)

Value

a dataframe with one row per team (twice as long as the input dataframe)

Examples

# \donttest{
# creating a small example dataframe!
cols <- c("season", "week", "home_team", "home_score",
          "away_team", "away_score", "result", "spread_line")

x <- as.data.frame(load_schedules(2020))
x <- utils::head(x[cols])

# how the data looks like
x
#>   season week home_team home_score away_team away_score result spread_line
#> 1   2020    1        KC         34       HOU         20     14         9.5
#> 2   2020    1       ATL         25       SEA         38    -13         1.0
#> 3   2020    1       BAL         38       CLE          6     32         7.0
#> 4   2020    1       BUF         27       NYJ         17     10         6.5
#> 5   2020    1       CAR         30        LV         34     -4        -3.0
#> 6   2020    1       DET         23       CHI         27     -4         2.5

clean_homeaway(x, invert = c("result","spread_line"))
#> # A tibble: 12 × 9
#>    season  week team  team_score opponent opponent_score result spread_line
#>     <int> <int> <chr>      <int> <chr>             <int>  <dbl>       <dbl>
#>  1   2020     1 KC            34 HOU                  20     14         9.5
#>  2   2020     1 HOU           20 KC                   34    -14        -9.5
#>  3   2020     1 ATL           25 SEA                  38    -13         1  
#>  4   2020     1 SEA           38 ATL                  25     13        -1  
#>  5   2020     1 BAL           38 CLE                   6     32         7  
#>  6   2020     1 CLE            6 BAL                  38    -32        -7  
#>  7   2020     1 BUF           27 NYJ                  17     10         6.5
#>  8   2020     1 NYJ           17 BUF                  27    -10        -6.5
#>  9   2020     1 CAR           30 LV                   34     -4        -3  
#> 10   2020     1 LV            34 CAR                  30      4         3  
#> 11   2020     1 DET           23 CHI                  27     -4         2.5
#> 12   2020     1 CHI           27 DET                  23      4        -2.5
#> # … with 1 more variable: location <chr>
# }