games <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-02-04/games.csv')
## Rows: 5324 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): week, home_team, away_team, winner, tie, day, date, home_team_nam...
## dbl (7): year, pts_win, pts_loss, yds_win, turnovers_win, yds_loss, turnov...
## time (1): time
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Case of numeric variables
games_small <- games %>% select(year, pts_win, pts_loss, yds_win, turnovers_win, yds_loss, turnovers_loss)
games_small %>% map_dbl(.x = ., .f = ~mean(x = .x))
## year pts_win pts_loss yds_win turnovers_win
## 2009.527047 27.781555 16.088843 361.635612 1.080203
## yds_loss turnovers_loss
## 309.080579 2.167543
When you have a grouping variable (factor)
games %>% lm(formula = yds_win ~ turnovers_win, data = .)
##
## Call:
## lm(formula = yds_win ~ turnovers_win, data = .)
##
## Coefficients:
## (Intercept) turnovers_win
## 359.142 2.309
games %>%
split(.$day) %>%
map(.x = ., .f = ~lm(formula = yds_win ~ turnovers_win, data = .)) %>%
map(broom::tidy)
## $Fri
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 462 24.7 18.7 0.0341
## 2 turnovers_win -19.5 30.3 -0.643 0.636
##
## $Mon
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 364. 5.81 62.7 6.04e-188
## 2 turnovers_win 0.0669 3.91 0.0171 9.86e- 1
##
## $Sat
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 352. 8.01 43.9 8.16e-97
## 2 turnovers_win 11.2 5.45 2.06 4.10e- 2
##
## $Sun
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 359. 1.70 212. 0
## 2 turnovers_win 1.97 1.12 1.75 0.0799
##
## $Thu
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 354. 6.73 52.5 1.62e-123
## 2 turnovers_win 9.11 5.53 1.65 1.01e- 1
##
## $Tue
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 337 NaN NaN NaN
## 2 turnovers_win NA NA NA NA
##
## $Wed
## # A tibble: 2 × 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 433 NaN NaN NaN
## 2 turnovers_win NA NA NA NA