Skip to content

Instantly share code, notes, and snippets.

@Ryo-N7
Last active December 7, 2023 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ryo-N7/4bc3bc677648336abd6f71a22dc1fca4 to your computer and use it in GitHub Desktop.
Save Ryo-N7/4bc3bc677648336abd6f71a22dc1fca4 to your computer and use it in GitHub Desktop.
Check for specific sequence of actions in StatsBomb event data
library(soccermatics)
library(StatsBombR)
library(tidyverse)
library(ggsoccer)
matchesdf <- FreeMatches(FreeCompetitions()) %>% filter(competition.competition_id == 11, season.season_id == 90)
barhue <- matchesdf %>% filter(match_id == 3773369)
barhue_df <- get.matchFree(barhue) %>% allclean()
barhue_df$type.name %>% unique()
## FOR ONE team, find this consecutive sequence in a single possession:
## Ball Receipt* >>> Pass >>> Ball Receipt*
## Ball Receipt* >>> Dribble >>> Ball Receipt*
## Index: check that we're looking at consecutive events
## minutue, second, period: more contextual checks
barhue_df %>% glimpse()
barc_df <- barhue_df %>%
filter(possession_team.name == "Barcelona") %>%
select(possession, type.name, period, index, minute, second)
# barc_df %>%
# rowwise() %>%
# mutate(seq = list(seq(type.name))) %>% View()
#
# barc_WIDE <- barc_df %>%
# pivot_wider(names_from = index, values_from = type.name)
barc_nested <- barc_df %>%
group_by(possession) %>%
summarize(action_list = list(type.name)) %>%
ungroup()
# barc_nested %>%
# filter(action_list == list("Ball Receipt*", "Pass", "Ball Receipt*"))
#
# barc_nested %>%
# mutate(tf = case_when(
# action_list %in% list("Ball Receipt*", "Pass", "Ball Receipt*") ~ TRUE,
# TRUE ~ FALSE
# )) %>% View()
# https://stackoverflow.com/questions/61620689/checking-for-sequences-in-an-r-vector
# find_subseq <- function(x, vec) {
# p <- match(x[1], vec)
#
# if (is.na(p)||length(x) == 1) {
# p } else {
# c(p, p + find_subseq(x[-1], vec[-seq_len(p)]))
# }
# }
#
# find_subseq(barc_nested$action_list[[10]], c("Ball Receipt*", "Pass", "Ball Receipt*")) ## FALSE
#
# find_subseq(barc_nested$action_list[[47]], c("Ball Receipt*", "Pass", "Ball Receipt*")) ## TRUE
# https://stackoverflow.com/questions/61620689/checking-for-sequences-in-an-r-vector
subseq_find <- function(x, y) {
inds <- which(x == head(y, 1))
if (length(inds) == 0) return(FALSE)
any(sapply(inds, function(k) all(x[k:(k + length(y) - 1)] == y)))
}
subseq_find(barc_nested$action_list[[47]], c("Ball Receipt*", "Pass", "Ball Receipt*")) ## TRUE
subseq_find(barc_nested$action_list[[5]], c("Ball Receipt*", "Pass", "Ball Receipt*")) ## FALSE
barc_seq_find1 <- barc_nested %>%
mutate(
has_seq = map(action_list, ~ subseq_find(x = .x, c("Ball Receipt*", "Pass", "Ball Receipt*"))),
has_seq = unlist(has_seq)
)
barc_seq_find2 <- barc_nested %>%
mutate(
has_seq = map(action_list, ~ subseq_find(x = .x, c("Ball Receipt*", "Dribble", "Ball Receipt*"))),
has_seq = unlist(has_seq)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment