-
Notifications
You must be signed in to change notification settings - Fork 0
/
BR-2-Base-Dist.R
48 lines (43 loc) · 2.02 KB
/
BR-2-Base-Dist.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Fielder to base distance
library(tidyverse)
source("Position-At-Throw.R")
distance <- function(x1, x2, y1, y2) {
output <- sqrt((x2-x1)^2 + (y2-y1)^2)
return(output)
}
br_distances <- position_at_throw %>%
arrange(game_str, play_id) %>%
# Baserunners excluding batters
filter(player_position %in% c(11,12,13)) %>%
select(game_str, play_id, player_position, field_x, field_y) %>%
mutate(BR2Second = distance(x1 = field_x,
y1 = field_y,
x2 = 0,
y2 = 90*sqrt(2)),
BR2Third = distance(x1 = field_x,
y1 = field_y,
x2 = -1*sqrt(4050),
y2 = sqrt(4050)),
BR2Home = distance(x1 = field_x,
y1 = field_y,
x2 = 0,
y2 = 0)) %>%
# Need to go back and case_when for if they've already passed a base
# Doing this in 2 separate steps because it's easier to understand
mutate(BR2Second = case_when(player_position %in% c(11) ~ BR2Second,
TRUE ~ 0),
BR2Third = case_when(player_position %in% c(11,12) ~ BR2Third,
TRUE ~ 0),
BR2Home = case_when(player_position %in% c(11,12, 13) ~ BR2Home,
TRUE ~ 0)) %>%
# Distance to the next base (or 2nd base for batters)
mutate(BR2NextBase = case_when(player_position %in% c(11) ~ BR2Second,
player_position == 12 ~ BR2Third,
player_position == 13 ~ BR2Home)) %>%
filter(!is.na(BR2Home)) %>%
select(game_str:player_position, BR2NextBase) %>%
# Make currentBase variable
mutate(currentBase = case_when(player_position == 11 ~ 1,
player_position == 12 ~ 2,
player_position == 13 ~ 3))
rm(position_at_throw, distance)