Here is what I get.
library(dplyr)
pc3 <- structure(list(Gm = c(36, 18, 26, 30, 14, 3, 32, 6, 20),
Date = c("2023-04-27", "2023-04-10", "2023-04-03", "2023-04-07", "2023-05-16", "2023-04-18",
"2023-04-04", "2023-04-22", "2023-04-28"),
Pitcher = c("Brooks Raley",
"Max Scherzer", "Tommy Hunter", "Dennis Santana", "Justin Verlander",
"Tylor Megill", "Brooks Raley", "David Peterson", "David Peterson"
),
Opp = c("WSN", "SDP", "@MIL", "MIA", "TBR", "@LAD", "@MIL",
"@SFG", "ATL"),
Batter = c("CJ Abrams", "Austin Nola", "Brice Turang",
"Garrett Cooper", "Isaac Paredes", "J.D. Martinez", "Brian Anderson",
"Brandon Crawford", "Matt Olson"),
Score = c("ahead 7-4", "ahead 2-0",
"down 0-6", "ahead 6-0", "tied 0-0", "tied 0-0", "down 0-5",
"down 0-1", "down 0-1"),
Event = c("HR", "SO", "HR", "HR", "HR",
"HR", "HR", "HR", "HR"),
Inn = c("t8", "t5", "b5", "t8", "t3",
"b1", "b7", "b1", "t5"),
RoB = c("123", "1--", "123", "12-",
"12-", "1--", "12-", "12-", "1-3"),
Out = c(1, 2, 1, 2, 2, 1,
2, 2, 2),
Pitcnt = c("2 (0-1)", "11 (3-2)", "2 (0-1)", "8 (3-2)",
"6 (3-2)", "10 (3-2)", "2 (0-1)", "6 (3-2)", "1 (0-0)"),
R = c(4, 0, 4, 3, 3, 2, 3, 3, 3),
WPA = c(-0.54, 0.03, -0.01,
-0.02, -0.29, -0.16, -0.01, -0.21, -0.25),
RE24 = c(-2.71,
0.22, -2.7, -2.68, -2.68, -1.73, -2.67, -2.67, -2.62),
LI = c(3.9,
0.98, 0.07, 0.16, 1.89, 1.15, 0.08, 1.42, 1.91),
PlayDesc = c("Home Run (Fly Ball to Deep CF-RF); Thomas Scores/unER; Garrett Scores; Robles Scores",
"Strikeout Swinging", "Home Run (Fly Ball to Deep CF-RF); Tellez Scores; Mitchell Scores; Anderson Scores",
"Home Run (Fly Ball to Deep CF-RF); Arraez Scores; Soler Scores",
"Home Run (Fly Ball to Deep LF); Ramírez Scores; Franco Scores",
"Home Run (Fly Ball to Deep CF-RF); Freeman Scores", "Home Run (Fly Ball to Deep CF); Yelich Scores; Adames Scores",
"Home Run (Fly Ball to Deep RF Line); Conforto Scores; Flores Scores",
"Home Run (Fly Ball to Deep CF-RF); Harris Scores; Acuña Scores"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-9L))
str.regex.pattern <- "(\\d+)\\s\\((\\d+)\\-(\\d+)\\)"
PITCHES <- pc3 %>% mutate(
pitches = gsub(str.regex.pattern, "\\1", Pitcnt),
x = gsub(str.regex.pattern, "\\2", Pitcnt),
y = gsub(str.regex.pattern, "\\3", Pitcnt)
)
PITCHES[, c("Pitcnt", "pitches", "x","y")]
#> # A tibble: 9 × 4
#> Pitcnt pitches x y
#> <chr> <chr> <chr> <chr>
#> 1 2 (0-1) 2 0 1
#> 2 11 (3-2) 11 3 2
#> 3 2 (0-1) 2 0 1
#> 4 8 (3-2) 8 3 2
#> 5 6 (3-2) 6 3 2
#> 6 10 (3-2) 10 3 2
#> 7 2 (0-1) 2 0 1
#> 8 6 (3-2) 6 3 2
#> 9 1 (0-0) 1 0 0
Created on 2023-05-27 with reprex v2.0.2