data("mtcars")
mtcars <- as_tibble(mtcars)
Case of numeric variables
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(.f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(.f = mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
# Adding an argument
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% map_dbl(mean, trim = 0.1)
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% select(.data = ., mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # ℹ 22 more rows
mtcars %>% select(mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # ℹ 22 more rows
Create your own function
# Double values in columns
double_by_vector <- function(x, factor) {x * factor}
10 %>% double_by_vector(factor = 2)
## [1] 20
100 %>% double_by_vector(factor = 2)
## [1] 200
mtcars %>% map_dfr(.x = ., .f = ~double_by_vector(x = .x, factor = 10))
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 210 60 1600 1100 39 26.2 165. 0 10 40 40
## 2 210 60 1600 1100 39 28.8 170. 0 10 40 40
## 3 228 40 1080 930 38.5 23.2 186. 10 10 40 10
## 4 214 60 2580 1100 30.8 32.2 194. 10 0 30 10
## 5 187 80 3600 1750 31.5 34.4 170. 0 0 30 20
## 6 181 60 2250 1050 27.6 34.6 202. 10 0 30 10
## 7 143 80 3600 2450 32.1 35.7 158. 0 0 30 40
## 8 244 40 1467 620 36.9 31.9 200 10 0 40 20
## 9 228 40 1408 950 39.2 31.5 229 10 0 40 20
## 10 192 60 1676 1230 39.2 34.4 183 10 0 40 40
## # ℹ 22 more rows
mtcars %>% map_dfr(double_by_vector, factor = 10)
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 210 60 1600 1100 39 26.2 165. 0 10 40 40
## 2 210 60 1600 1100 39 28.8 170. 0 10 40 40
## 3 228 40 1080 930 38.5 23.2 186. 10 10 40 10
## 4 214 60 2580 1100 30.8 32.2 194. 10 0 30 10
## 5 187 80 3600 1750 31.5 34.4 170. 0 0 30 20
## 6 181 60 2250 1050 27.6 34.6 202. 10 0 30 10
## 7 143 80 3600 2450 32.1 35.7 158. 0 0 30 40
## 8 244 40 1467 620 36.9 31.9 200 10 0 40 20
## 9 228 40 1408 950 39.2 31.5 229 10 0 40 20
## 10 192 60 1676 1230 39.2 34.4 183 10 0 40 40
## # ℹ 22 more rows
When you have a grouping variable (factor)
mtcars %>% lm(formula = mpg ~ wt, data = .)
##
## Call:
## lm(formula = mpg ~ wt, data = .)
##
## Coefficients:
## (Intercept) wt
## 37.285 -5.344
mtcars %>% distinct(cyl)
## # A tibble: 3 × 1
## cyl
## <dbl>
## 1 6
## 2 4
## 3 8
reg_coeff_tbl <- mtcars %>%
# Split it into a list of dataf rames
split(.$cyl) %>%
# Repeat regression over each group
map(~lm(formula = mpg ~ wt, data = .x)) %>%
# Extract coefficients from regression results
map(broom::tidy, conf.int = TRUE) %>%
# Convert to tibble
bind_rows(.id = "cyl") %>%
# Filter or wt coefficients
filter(term == "wt")
reg_coeff_tbl %>%
mutate(estimate = -estimate,
conf.low = -conf.low,
conf.high = -conf.high) %>%
ggplot(aes(x = estimate, y = cyl)) +
geom_point() +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high))
Choose either one of the two cases above and apply it to your data
# excel filer
games <- read_excel("../00_data/MyData_charts.xlsx")
games
## # A tibble: 988 × 15
## year country city stage home_team away_team home_score away_score outcome
## <dbl> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 1930 Uruguay Montev… Grou… France Mexico 4 1 H
## 2 1930 Uruguay Montev… Grou… Belgium United S… 0 3 A
## 3 1930 Uruguay Montev… Grou… Brazil Yugoslav… 1 2 A
## 4 1930 Uruguay Montev… Grou… Peru Romania 1 3 A
## 5 1930 Uruguay Montev… Grou… Argentina France 1 0 H
## 6 1930 Uruguay Montev… Grou… Chile Mexico 3 0 H
## 7 1930 Uruguay Montev… Grou… Bolivia Yugoslav… 0 4 A
## 8 1930 Uruguay Montev… Grou… Paraguay United S… 0 3 A
## 9 1930 Uruguay Montev… Grou… Uruguay Peru 1 0 H
## 10 1930 Uruguay Montev… Grou… Argentina Mexico 6 3 H
## # ℹ 978 more rows
## # ℹ 6 more variables: win_conditions <chr>, winning_team <chr>,
## # losing_team <chr>, date <dttm>, month <chr>, dayofweek <chr>
## Clean Data
games1 <- na.omit(games[, c("year", "country", "city", "stage", "home_team", "away_team", "home_score", "away_score", "outcome", "winning_team", "losing_team", "date", "month", "dayofweek")])
games1
## # A tibble: 900 × 14
## year country city stage home_team away_team home_score away_score outcome
## <dbl> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <chr>
## 1 1930 Uruguay Montev… Grou… France Mexico 4 1 H
## 2 1930 Uruguay Montev… Grou… Belgium United S… 0 3 A
## 3 1930 Uruguay Montev… Grou… Brazil Yugoslav… 1 2 A
## 4 1930 Uruguay Montev… Grou… Peru Romania 1 3 A
## 5 1930 Uruguay Montev… Grou… Argentina France 1 0 H
## 6 1930 Uruguay Montev… Grou… Chile Mexico 3 0 H
## 7 1930 Uruguay Montev… Grou… Bolivia Yugoslav… 0 4 A
## 8 1930 Uruguay Montev… Grou… Paraguay United S… 0 3 A
## 9 1930 Uruguay Montev… Grou… Uruguay Peru 1 0 H
## 10 1930 Uruguay Montev… Grou… Argentina Mexico 6 3 H
## # ℹ 890 more rows
## # ℹ 5 more variables: winning_team <chr>, losing_team <chr>, date <dttm>,
## # month <chr>, dayofweek <chr>
games55 <- na.omit(games[, c("year", "home_score", "away_score")])
games55
## # A tibble: 900 × 3
## year home_score away_score
## <dbl> <dbl> <dbl>
## 1 1930 4 1
## 2 1930 0 3
## 3 1930 1 2
## 4 1930 1 3
## 5 1930 1 0
## 6 1930 3 0
## 7 1930 0 4
## 8 1930 0 3
## 9 1930 1 0
## 10 1930 6 3
## # ℹ 890 more rows
double_by_vector <- function(x, factor) {x * factor}
10 %>% double_by_vector(factor = 2)
## [1] 20
100 %>% double_by_vector(factor = 2)
## [1] 200
games55 %>% map_dfr(.x = ., .f = ~double_by_vector(x = .x, factor = 10))
## # A tibble: 900 × 3
## year home_score away_score
## <dbl> <dbl> <dbl>
## 1 19300 40 10
## 2 19300 0 30
## 3 19300 10 20
## 4 19300 10 30
## 5 19300 10 0
## 6 19300 30 0
## 7 19300 0 40
## 8 19300 0 30
## 9 19300 10 0
## 10 19300 60 30
## # ℹ 890 more rows
games55 %>% map_dfr(double_by_vector, factor = 10)
## # A tibble: 900 × 3
## year home_score away_score
## <dbl> <dbl> <dbl>
## 1 19300 40 10
## 2 19300 0 30
## 3 19300 10 20
## 4 19300 10 30
## 5 19300 10 0
## 6 19300 30 0
## 7 19300 0 40
## 8 19300 0 30
## 9 19300 10 0
## 10 19300 60 30
## # ℹ 890 more rows
games4 <- games1 %>%
filter(home_team %in% c("Sweden", "United States", "Belgium")) %>%
select(home_team, home_score) %>%
pivot_longer(cols = c("home_team"),
names_to = "home_or_away",
values_to = "hometeam")
standard_deviation <- games4 %>%
group_by(hometeam) %>%
summarise(total_home_score = sum(home_score),
stddev_home_score = sd(home_score))
# View the summary with standard deviation
print(standard_deviation)
## # A tibble: 3 × 3
## hometeam total_home_score stddev_home_score
## <chr> <dbl> <dbl>
## 1 Belgium 45 1.28
## 2 Sweden 38 1.12
## 3 United States 12 0.954
games5 <- games4 %>%
group_by(hometeam) %>%
summarise(total_home_score = sum(home_score),
mean_home_score = mean(home_score),
stddev_home_score = sd(home_score))
# Calculate confidence interval (using default alpha = 0.05)
games6 <- games5 %>%
mutate(conf_int_lower = mean_home_score - qt(1 - 0.05 / 2, n() - 1) * (stddev_home_score / sqrt(n())),
conf_int_upper = mean_home_score + qt(1 - 0.05 / 2, n() - 1) * (stddev_home_score / sqrt(n())))
# View the summary with confidence interval
print(games6)
## # A tibble: 3 × 6
## hometeam total_home_score mean_home_score stddev_home_score conf_int_lower
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Belgium 45 1.73 1.28 -1.45
## 2 Sweden 38 1.52 1.12 -1.27
## 3 United Stat… 12 0.923 0.954 -1.45
## # ℹ 1 more variable: conf_int_upper <dbl>
games6 %>%
ggplot(aes(x = mean_home_score, y = hometeam)) +
geom_point() +
geom_errorbar(aes(xmin = conf_int_lower, xmax = conf_int_upper), width = 0.4) +
labs(x = "Goals", y = "Team", title = "Mean Home Scores with Confidence Intervals")
sweden_homegoal <- games1 %>%
mutate(col_Sweden = str_extract(home_team, "Sweden")) %>%
select(col_Sweden, home_score) %>%
filter(!is.na(col_Sweden))
sweden_homegoal
## # A tibble: 25 × 2
## col_Sweden home_score
## <chr> <dbl>
## 1 Sweden 2
## 2 Sweden 3
## 3 Sweden 2
## 4 Sweden 0
## 5 Sweden 2
## 6 Sweden 3
## 7 Sweden 2
## 8 Sweden 1
## 9 Sweden 0
## 10 Sweden 3
## # ℹ 15 more rows
us_homegoal <- games1 %>%
mutate(col_Us = str_extract(home_team, "United States")) %>%
select(col_Us, home_score) %>%
filter(!is.na(col_Us))
us_homegoal
## # A tibble: 13 × 2
## col_Us home_score
## <chr> <dbl>
## 1 United States 1
## 2 United States 1
## 3 United States 2
## 4 United States 0
## 5 United States 0
## 6 United States 1
## 7 United States 0
## 8 United States 3
## 9 United States 0
## 10 United States 1
## 11 United States 1
## 12 United States 2
## 13 United States 0
sweden_homegoal %>%
summarise(sum(home_score))
## # A tibble: 1 × 1
## `sum(home_score)`
## <dbl>
## 1 38
us_homegoal %>%
summarise(sum(home_score))
## # A tibble: 1 × 1
## `sum(home_score)`
## <dbl>
## 1 12
combined_homegoals <- list(sweden_homegoal, us_homegoal)
sweden <- games1 %>%
filter(home_team == "Sweden") %>%
select(home_team, home_score) %>% head(10)
sweden
## # A tibble: 10 × 2
## home_team home_score
## <chr> <dbl>
## 1 Sweden 2
## 2 Sweden 3
## 3 Sweden 2
## 4 Sweden 0
## 5 Sweden 2
## 6 Sweden 3
## 7 Sweden 2
## 8 Sweden 1
## 9 Sweden 0
## 10 Sweden 3
us <- games1 %>%
filter(home_team == "United States") %>%
select(home_team, home_score) %>% head(10)
us
## # A tibble: 10 × 2
## home_team home_score
## <chr> <dbl>
## 1 United States 1
## 2 United States 1
## 3 United States 2
## 4 United States 0
## 5 United States 0
## 6 United States 1
## 7 United States 0
## 8 United States 3
## 9 United States 0
## 10 United States 1
combined1 <- cbind(sweden, us)
colnames(combined1) <- c("country", "goals1", "country", "goals2")
combined1 %>% lm(formula = goals1 ~ goals2, data = .)
##
## Call:
## lm(formula = goals1 ~ goals2, data = .)
##
## Coefficients:
## (Intercept) goals2
## 1.6180 0.2022
combined1 %>% distinct()
## country goals1 country.1 goals2
## 1 Sweden 2 United States 1
## 2 Sweden 3 United States 1
## 3 Sweden 2 United States 2
## 4 Sweden 0 United States 0
## 5 Sweden 2 United States 0
## 6 Sweden 1 United States 3