edit data cleaning and add final data

This commit is contained in:
LukaszChrostowski 2023-12-11 23:45:53 +01:00
parent ca199213c7
commit d23b1675e4
5 changed files with 38478 additions and 405 deletions

BIN
.DS_Store vendored

Binary file not shown.

451
.Rhistory
View File

@ -1,388 +1,16 @@
error = function(e) { data4 <- read.csv("data/data4.csv")
# handle the error setwd("~/Desktop/fantastyczne_gole")
print(paste("An error occurred:", e$message)) data4 <- read.csv("data/data4.csv")
}) nrow
data$shot <- data$shot %>% select(-freeze_frame, -statsbomb_xg, -key_pass_id) nrow(data4)
data$shot$body_part <- data$shot$body_part %>% select(-id) data_list
data$shot$technique <- data$shot$technique %>% select(-id)
data$shot$type <- data$shot$type %>% select(-id)
data$position <- data$position %>% select(-id)
data$shot <- data$shot %>% select(-end_location)
tryCatch({ # TODO reduce error cases
if ("one_on_one" %in% colnames(data$shot)) {
data[is.na(data$shot$one_on_one), ]$shot$one_on_one <- FALSE
} else {
data$shot$one_on_one <- FALSE
}
if ("first_time" %in% colnames(data$shot)) {
data[is.na(data$shot$first_time), ]$shot$first_time <- FALSE
} else {
data$shot$first_time <- FALSE
}
if ("aerial_won" %in% colnames(data$shot)) {
data[is.na(data$shot$aerial_won), ]$shot$aerial_won <- FALSE
} else {
data$shot$aerial_won <- FALSE
}
if ("saved_to_post" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_to_post), ]$shot$saved_to_post <- FALSE
} else {
data$shot$saved_to_post <- FALSE
}
if ("deflected" %in% colnames(data$shot)) {
data[is.na(data$shot$deflected), ]$shot$deflected <- FALSE
} else {
data$shot$deflected <- FALSE
}
if ("saved_off_target" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_off_target), ]$shot$saved_off_target <- FALSE
} else {
data$shot$saved_off_target <- FALSE
}
if ("open_goal" %in% colnames(data$shot)) {
data[is.na(data$shot$open_goal), ]$shot$open_goal <- FALSE
} else {
data$shot$open_goal <- FALSE
}
if ("follows_dribble" %in% colnames(data$shot)) {
data[is.na(data$shot$follows_dribble), ]$shot$follows_dribble <- FALSE
} else {
data$shot$follows_dribble <- FALSE
}
if ("redirect" %in% colnames(data$shot)) {
data[is.na(data$shot$redirect), ]$shot$redirect <- FALSE
} else {
data$shot$redirect <- FALSE
}
if ("kick_off" %in% colnames(data$kick_off)) {
data[is.na(data$kick_off$kick_off), ]$kick_off$kick_off <- FALSE
} else {
data$kick_off$kick_off <- FALSE
}
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data <- data %>% mutate(angle = atan(7.32 * x1 / (x1^2 + y1^2 - (7.32/2)^2)),
is_goal = ifelse(shot$outcome$id == 97, 1, 0)) %>%
select(-location)
data$shot$outcome <- data$shot$outcome %>% select(-id)
data <- data %>% unnest(shot, names_sep = "_")
data$angle <- ifelse(data$angle<0, base::pi + data$angle, data$angle)
data
}
data_test <- get_shots2("data/la_liga_events/303377.json")
View(data_test)
# TODO not all json data have these same keys/fields, customize function to cover all cases
get_shots2 <- function(json_file) {
data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot))
df_temp <- do.call(rbind, data$location)
colnames(df_temp) <- c("x1", "y1")
data$x1 <- df_temp[,1]
data$y1 <- df_temp[,2]
tryCatch({ # TODO reduce error cases
data$number_of_players <- mapply(function(sublist, x1_threshold) {
# Extracting the first location value and converting it to numeric
first_location_values <- sapply(sublist$location, function(loc) as.numeric(loc[1]))
if ("teammate" %in% names(sublist)) {
# Filtering and counting
res <- sum(!sublist$teammate & first_location_values > x1_threshold) # error here
} else {
res <- 0
}
res
}, data$shot$freeze_frame, data$x1)
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data$shot <- data$shot %>% select(-freeze_frame, -statsbomb_xg, -key_pass_id)
data$shot$body_part <- data$shot$body_part %>% select(-id)
data$shot$technique <- data$shot$technique %>% select(-id)
data$shot$type <- data$shot$type %>% select(-id)
data$position <- data$position %>% select(-id)
data$shot <- data$shot %>% select(-end_location)
tryCatch({ # TODO reduce error cases
if ("one_on_one" %in% colnames(data$shot)) {
data[is.na(data$shot$one_on_one), ]$shot$one_on_one <- FALSE
} else {
data$shot$one_on_one <- FALSE
}
if ("first_time" %in% colnames(data$shot)) {
data[is.na(data$shot$first_time), ]$shot$first_time <- FALSE
} else {
data$shot$first_time <- FALSE
}
if ("aerial_won" %in% colnames(data$shot)) {
data[is.na(data$shot$aerial_won), ]$shot$aerial_won <- FALSE
} else {
data$shot$aerial_won <- FALSE
}
if ("saved_to_post" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_to_post), ]$shot$saved_to_post <- FALSE
} else {
data$shot$saved_to_post <- FALSE
}
if ("deflected" %in% colnames(data$shot)) {
data[is.na(data$shot$deflected), ]$shot$deflected <- FALSE
} else {
data$shot$deflected <- FALSE
}
if ("saved_off_target" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_off_target), ]$shot$saved_off_target <- FALSE
} else {
data$shot$saved_off_target <- FALSE
}
if ("open_goal" %in% colnames(data$shot)) {
data[is.na(data$shot$open_goal), ]$shot$open_goal <- FALSE
} else {
data$shot$open_goal <- FALSE
}
if ("follows_dribble" %in% colnames(data$shot)) {
data[is.na(data$shot$follows_dribble), ]$shot$follows_dribble <- FALSE
} else {
data$shot$follows_dribble <- FALSE
}
if ("redirect" %in% colnames(data$shot)) {
data[is.na(data$shot$redirect), ]$shot$redirect <- FALSE
} else {
data$shot$redirect <- FALSE
}
if ("kick_off" %in% colnames(data$kick_off)) {
data[is.na(data$kick_off$kick_off), ]$kick_off$kick_off <- FALSE
} else {
data$kick_off$kick_off <- FALSE
}
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data <- data %>% mutate(angle = atan(7.32 * x1 / (x1^2 + y1^2 - (7.32/2)^2)),
is_goal = ifelse(shot$outcome$id == 97, 1, 0)) %>%
select(-location)
data$angle <- ifelse(data$angle<0, base::pi + data$angle, data$angle)
data$shot$outcome <- data$shot$outcome %>% select(-id)
data <- data %>% unnest(shot, names_sep = "_")
%>% unnest(position, names_sep = "_")
# TODO not all json data have these same keys/fields, customize function to cover all cases
get_shots2 <- function(json_file) {
data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot))
df_temp <- do.call(rbind, data$location)
colnames(df_temp) <- c("x1", "y1")
data$x1 <- df_temp[,1]
data$y1 <- df_temp[,2]
tryCatch({ # TODO reduce error cases
data$number_of_players <- mapply(function(sublist, x1_threshold) {
# Extracting the first location value and converting it to numeric
first_location_values <- sapply(sublist$location, function(loc) as.numeric(loc[1]))
if ("teammate" %in% names(sublist)) {
# Filtering and counting
res <- sum(!sublist$teammate & first_location_values > x1_threshold) # error here
} else {
res <- 0
}
res
}, data$shot$freeze_frame, data$x1)
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data$shot <- data$shot %>% select(-freeze_frame, -statsbomb_xg, -key_pass_id)
data$shot$body_part <- data$shot$body_part %>% select(-id)
data$shot$technique <- data$shot$technique %>% select(-id)
data$shot$type <- data$shot$type %>% select(-id)
data$position <- data$position %>% select(-id)
data$shot <- data$shot %>% select(-end_location)
tryCatch({ # TODO reduce error cases
if ("one_on_one" %in% colnames(data$shot)) {
data[is.na(data$shot$one_on_one), ]$shot$one_on_one <- FALSE
} else {
data$shot$one_on_one <- FALSE
}
if ("first_time" %in% colnames(data$shot)) {
data[is.na(data$shot$first_time), ]$shot$first_time <- FALSE
} else {
data$shot$first_time <- FALSE
}
if ("aerial_won" %in% colnames(data$shot)) {
data[is.na(data$shot$aerial_won), ]$shot$aerial_won <- FALSE
} else {
data$shot$aerial_won <- FALSE
}
if ("saved_to_post" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_to_post), ]$shot$saved_to_post <- FALSE
} else {
data$shot$saved_to_post <- FALSE
}
if ("deflected" %in% colnames(data$shot)) {
data[is.na(data$shot$deflected), ]$shot$deflected <- FALSE
} else {
data$shot$deflected <- FALSE
}
if ("saved_off_target" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_off_target), ]$shot$saved_off_target <- FALSE
} else {
data$shot$saved_off_target <- FALSE
}
if ("open_goal" %in% colnames(data$shot)) {
data[is.na(data$shot$open_goal), ]$shot$open_goal <- FALSE
} else {
data$shot$open_goal <- FALSE
}
if ("follows_dribble" %in% colnames(data$shot)) {
data[is.na(data$shot$follows_dribble), ]$shot$follows_dribble <- FALSE
} else {
data$shot$follows_dribble <- FALSE
}
if ("redirect" %in% colnames(data$shot)) {
data[is.na(data$shot$redirect), ]$shot$redirect <- FALSE
} else {
data$shot$redirect <- FALSE
}
if ("kick_off" %in% colnames(data$kick_off)) {
data[is.na(data$kick_off$kick_off), ]$kick_off$kick_off <- FALSE
} else {
data$kick_off$kick_off <- FALSE
}
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data <- data %>% mutate(angle = atan(7.32 * x1 / (x1^2 + y1^2 - (7.32/2)^2)),
is_goal = ifelse(shot$outcome$id == 97, 1, 0)) %>%
select(-location)
data$angle <- ifelse(data$angle<0, base::pi + data$angle, data$angle)
data$shot$outcome <- data$shot$outcome %>% select(-id)
data <- data %>% unnest(shot, names_sep = "_") %>%
unnest(position, names_sep = "_") %>%
unnest(shot_type, names_sep = "_") %>%
unnest(shot_outcome, names_sep = "_") %>%
unnest(shot_technique, names_sep = "_")
data
}
data_test <- get_shots2("data/la_liga_events/303377.json")
View(data_test)
write.csv(data_test, file = "data/data_test.csv")
colnames(data_test)
data_test$kick_off
data_test$shot_body_part
data_df <- as.data.frame(data_test)
# TODO not all json data have these same keys/fields, customize function to cover all cases
get_shots2 <- function(json_file) {
data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot))
df_temp <- do.call(rbind, data$location)
colnames(df_temp) <- c("x1", "y1")
data$x1 <- df_temp[,1]
data$y1 <- df_temp[,2]
tryCatch({ # TODO reduce error cases
data$number_of_players <- mapply(function(sublist, x1_threshold) {
# Extracting the first location value and converting it to numeric
first_location_values <- sapply(sublist$location, function(loc) as.numeric(loc[1]))
if ("teammate" %in% names(sublist)) {
# Filtering and counting
res <- sum(!sublist$teammate & first_location_values > x1_threshold) # error here
} else {
res <- 0
}
res
}, data$shot$freeze_frame, data$x1)
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data$shot <- data$shot %>% select(-freeze_frame, -statsbomb_xg, -key_pass_id)
data$shot$body_part <- data$shot$body_part %>% select(-id)
data$shot$technique <- data$shot$technique %>% select(-id)
data$shot$type <- data$shot$type %>% select(-id)
data$position <- data$position %>% select(-id)
data$shot <- data$shot %>% select(-end_location)
tryCatch({ # TODO reduce error cases
if ("one_on_one" %in% colnames(data$shot)) {
data[is.na(data$shot$one_on_one), ]$shot$one_on_one <- FALSE
} else {
data$shot$one_on_one <- FALSE
}
if ("first_time" %in% colnames(data$shot)) {
data[is.na(data$shot$first_time), ]$shot$first_time <- FALSE
} else {
data$shot$first_time <- FALSE
}
if ("aerial_won" %in% colnames(data$shot)) {
data[is.na(data$shot$aerial_won), ]$shot$aerial_won <- FALSE
} else {
data$shot$aerial_won <- FALSE
}
if ("saved_to_post" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_to_post), ]$shot$saved_to_post <- FALSE
} else {
data$shot$saved_to_post <- FALSE
}
if ("deflected" %in% colnames(data$shot)) {
data[is.na(data$shot$deflected), ]$shot$deflected <- FALSE
} else {
data$shot$deflected <- FALSE
}
if ("saved_off_target" %in% colnames(data$shot)) {
data[is.na(data$shot$saved_off_target), ]$shot$saved_off_target <- FALSE
} else {
data$shot$saved_off_target <- FALSE
}
if ("open_goal" %in% colnames(data$shot)) {
data[is.na(data$shot$open_goal), ]$shot$open_goal <- FALSE
} else {
data$shot$open_goal <- FALSE
}
if ("follows_dribble" %in% colnames(data$shot)) {
data[is.na(data$shot$follows_dribble), ]$shot$follows_dribble <- FALSE
} else {
data$shot$follows_dribble <- FALSE
}
if ("redirect" %in% colnames(data$shot)) {
data[is.na(data$shot$redirect), ]$shot$redirect <- FALSE
} else {
data$shot$redirect <- FALSE
}
if ("kick_off" %in% colnames(data$kick_off)) {
data[is.na(data$shot$kick_off), ]$shotf$kick_off <- FALSE
} else {
data$kick_off <- FALSE
}
},
error = function(e) {
# handle the error
print(paste("An error occurred:", e$message))
})
data <- data %>% mutate(angle = atan(7.32 * x1 / (x1^2 + y1^2 - (7.32/2)^2)),
is_goal = ifelse(shot$outcome$id == 97, 1, 0)) %>%
select(-location)
data$angle <- ifelse(data$angle<0, base::pi + data$angle, data$angle)
data$shot$outcome <- data$shot$outcome %>% select(-id)
data <- data %>% unnest(shot, names_sep = "_") %>%
unnest(position, names_sep = "_") %>%
unnest(shot_type, names_sep = "_") %>%
unnest(shot_outcome, names_sep = "_") %>%
unnest(shot_technique, names_sep = "_") %>%
unnest(shot_body_part, names_sep = "_")
data
}
data_test <- get_shots2("data/la_liga_events/303377.json")
write.csv(data_test, file = "data/data_test.csv")
file_names <- list.files(path = "data/la_liga_events/", pattern = "*.json") file_names <- list.files(path = "data/la_liga_events/", pattern = "*.json")
length(file_names)
data_list <- lapply(paste("data/la_liga_events/", file_names, sep = ""), get_shots2) data_list <- lapply(paste("data/la_liga_events/", file_names, sep = ""), get_shots2)
combined_data <- do.call(rbind, data_list) library(jsonlite)
data_test <- data <- fromJSON(("data/la_liga_events/303377.json") %>% filter(type$name == "Shot") library(tidyverse)
data_test <- fromJSON("data/la_liga_events/303377.json") %>% filter(type$name == "Shot") library(ggsoccer)
data_test <- fromJSON("data/la_liga_events/303377.json") %>% filter(type$name == "Shot") library(dplyr)
data_test$id
data_test$index
data4 <- read.csv("data/data4.csv", nrows = 1000)
View(data4)
# TODO not all json data have these same keys/fields, customize function to cover all cases # TODO not all json data have these same keys/fields, customize function to cover all cases
get_shots2 <- function(json_file) { get_shots2 <- function(json_file) {
data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot)) data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot))
@ -499,14 +127,51 @@ unnest(shot_technique, names_sep = "_") %>%
unnest(shot_body_part, names_sep = "_") unnest(shot_body_part, names_sep = "_")
data data
} }
# sample data data_list <- lapply(paste("data/la_liga_events/", file_names, sep = ""), get_shots2)
data_test <- get_shots2("data/la_liga_events/303377.json") length(data_list)
write.csv(data_test, file = "data/data3_test.csv") ss <- lapply(data_list, nrow())
data1 <- read.csv("data/data1.csv", nrows = 1000) ss <- lapply(data_list, nrow)
ss
sum(ss)
sss <- unlist(ss)
sss
sum(sss)
source("notebooks/dataCleaning.R")
source("notebooks/dataCleaning.R")
# generate random data
random_data <- as.data.frame(x = rnorm(100),
y = rnorm(100, mean = 1, sd = 3))
# unit tests
expect_silent(
data <- get_shots(file_path = "data/events/events_England.json", name_detail = "EN")
)
library(tinytest)
# unit tests
expect_silent(
data <- get_shots(file_path = "data/events/events_England.json", name_detail = "EN")
)
expect_silent(
data <- get_shots2(json_file = "data/la_liga_events/ (1).json")
)
expect_error(
get_final_data(data = random_data)
)
data1 <- get_shots2(json_file = "data/la_liga_events/ (1).json")
data2 <- get_shots2(json_file = "data/la_liga_events/ (2).json")
expect_equivalent(colnames(data1), colnames(data2))
expect_false(is.vector(data1))
expect_true(is.data.frame(data1))
colnames(data1) colnames(data1)
View(data1) colnames(data2)
data2 <- read.csv("data/data2.csv", nrows = 1000) expect_length(length(colnames(data1)), length(colnames(data2)))
View(data2) length(colnames(data1))
data3 <- read.csv("data/data3_test.csv", nrows = 1000) length(colnames(data2))
colnames(data3) ?expect_length
View(data3) expect_length(colnames(data1), colnames(data2))
expect_length(colnames(data1), colnames(data2))
expect_length(colnames(data1), 22)
expect_length(colnames(data2), 22)
expect_false(is.vector(data1))
expect_true(is.data.frame(data1))
df <- read.csv("data/data4.csv", nrows = 1000)
View(df)

BIN
data/.DS_Store vendored

Binary file not shown.

38343
data/final_data.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,8 @@ library(jsonlite)
library(tidyverse) library(tidyverse)
library(ggsoccer) library(ggsoccer)
library(dplyr) library(dplyr)
library(REdaS)
library(yd2m)
# code and data from https://github.com/Dato-Futbol/xg-model # code and data from https://github.com/Dato-Futbol/xg-model
get_shots <- function(file_path, name_detail, save_files = F){ get_shots <- function(file_path, name_detail, save_files = F){
@ -99,21 +101,67 @@ get_data <- function(event_path, info_path) {
# data2 <- get_data(event_path = "data/events.csv", info_path = "data/ginf.csv") # data2 <- get_data(event_path = "data/events.csv", info_path = "data/ginf.csv")
# write.csv(data2, file = "data/data2.csv") # write.csv(data2, file = "data/data2.csv")
# TODO dodać kolumne z x y coordinates bazując na location
# TODO dodać kolumnę z kątem do bramki
##################### The third dataset ############################## ##################### The third dataset ##############################
# TODO not all json data have these same keys/fields, customize function to cover all cases # make angle from the x, y coordinares for the 3rd dataset
loc2angle <- function(x, y) {
rads <- atan(7.32 * x / (x^2 + (y - 34)^2 - (7.32/2)^2))
rads <- ifelse(rads<0, base::pi + rads, rads)
deg <- rad2deg(rads)
deg
}
get_shots2 <- function(json_file) { get_shots2 <- function(json_file) {
data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot)) data <- fromJSON(json_file) %>% filter(type$name == "Shot") %>% dplyr::select(c(minute, position, location, shot))
df_temp <- do.call(rbind, data$location) df_temp <- do.call(rbind, lapply(data$location, function(loc) c(120, 80) - loc))
colnames(df_temp) <- c("x1", "y1") colnames(df_temp) <- c("x1", "y1")
data$x1 <- df_temp[,1] data$x1 <- df_temp[,1]
data$y1 <- df_temp[,2] data$y1 <- df_temp[,2]
tryCatch({
df_players_location <- mapply( function(sublist) {
if (!is.null(sublist$teammate)) {
df_players <- sapply(sublist$location, function(loc) c(120, 80) - loc %>% as.numeric() %>% yd_to_m() %>% round(., digits = 1)) %>% t() %>% as.data.frame()
# df <- sapply(sublist$teammate, function(tmt) cbind(df_players, tmt))
df <- cbind(df_players, sublist$teammate)
colnames(df) <- c("x", "y", "teammate")
na_df <- as.data.frame(matrix(NA, nrow = 21 - nrow(df), ncol = ncol(df)))
colnames(na_df) <- colnames(df)
dff <- rbind(df, na_df)
} else {
dff <- as.data.frame(matrix(NA, nrow = 21, ncol = 3))
colnames(dff) <- c("x", "y", "teammate")
}
wider_df <- dff %>%
mutate(row = row_number()) %>%
pivot_wider(names_from = row, values_from = c(x, y, teammate), names_sep = "_player")
wider_df
}, data$shot$freeze_frame)
},
error = function(e) {
# handle the error
print(json_file)
print(paste("An error occurred:", e$message))
})
df_players_location <- df_players_location %>% t()
# df <- do.call(rbind, df_test$shot$freeze_frame[[1]]$location) %>% as.data.frame()
# dff <- cbind(df, df_test$shot$freeze_frame[[1]]$teammate)
# colnames(dff) <- c("x", "y", "teammate")
#
#
# na_df <- as.data.frame(matrix(NA, nrow = 21 - nrow(df), ncol = ncol(dff)))
# colnames(na_df) <- colnames(dff)
# dff <- rbind(dff, na_df)
#
# wider_df <- dff %>%
# mutate(row = row_number()) %>%
# pivot_wider(names_from = row, values_from = c(x, y, teammate), names_sep = "_player")
tryCatch({ # TODO reduce error cases tryCatch({ # TODO reduce error cases
data$number_of_players_opponents <- mapply(function(sublist, x1_threshold) { data$number_of_players_opponents <- mapply(function(sublist, x1_threshold) {
# Extracting the first location value and converting it to numeric # Extracting the first location value and converting it to numeric
@ -129,6 +177,7 @@ get_shots2 <- function(json_file) {
}, data$shot$freeze_frame, data$x1) }, data$shot$freeze_frame, data$x1)
}, },
error = function(e) { error = function(e) {
print(json_file)
# handle the error # handle the error
print(paste("An error occurred:", e$message)) print(paste("An error occurred:", e$message))
}) })
@ -148,6 +197,7 @@ get_shots2 <- function(json_file) {
}, data$shot$freeze_frame, data$x1) }, data$shot$freeze_frame, data$x1)
}, },
error = function(e) { error = function(e) {
print(json_file)
# handle the error # handle the error
print(paste("An error occurred:", e$message)) print(paste("An error occurred:", e$message))
}) })
@ -227,10 +277,11 @@ get_shots2 <- function(json_file) {
print(paste("An error occurred:", e$message)) print(paste("An error occurred:", e$message))
}) })
data <- data %>% mutate(angle = atan(7.32 * x1 / (x1^2 + y1^2 - (7.32/2)^2)), data <- data %>% mutate(is_goal = ifelse(shot$outcome$id == 97, 1, 0),
is_goal = ifelse(shot$outcome$id == 97, 1, 0)) %>% x1 = yd_to_m(x1) %>% round(., digits = 1),
y1 = yd_to_m(y1) %>% round(., digits = 1),
angle = loc2angle(x1, y1) %>% round(., digits = 1)) %>%
select(-location) select(-location)
data$angle <- ifelse(data$angle<0, base::pi + data$angle, data$angle)
data$shot$outcome <- data$shot$outcome %>% select(-id) data$shot$outcome <- data$shot$outcome %>% select(-id)
data <- data %>% unnest(shot, names_sep = "_") %>% data <- data %>% unnest(shot, names_sep = "_") %>%
unnest(position, names_sep = "_") %>% unnest(position, names_sep = "_") %>%
@ -238,20 +289,34 @@ get_shots2 <- function(json_file) {
unnest(shot_outcome, names_sep = "_") %>% unnest(shot_outcome, names_sep = "_") %>%
unnest(shot_technique, names_sep = "_") %>% unnest(shot_technique, names_sep = "_") %>%
unnest(shot_body_part, names_sep = "_") unnest(shot_body_part, names_sep = "_")
data <- cbind(data, df_players_location)
data data
} }
# file_names <- list.files(path = "data/la_liga_events/", pattern = "*.json") file_names <- list.files(path = "data/la_liga_events/", pattern = "*.json")
# data_list <- lapply(paste("data/la_liga_events/", file_names, sep = ""), get_shots2) data_list <- lapply(paste("data/la_liga_events/", file_names, sep = ""), get_shots2)
# combined_data <- do.call(rbind, data_list) combined_data <- do.call(bind_rows, data_list)
# ss <- lapply(data_list, nrow) # ss <- lapply(data_list, nrow)
# sss <- unlist(ss) # sss <- unlist(ss)
# #
# # sample data # # sample data
# data_test <- get_shots2("data/la_liga_events/303377.json") # data_test <- get_shots2("data/la_liga_events/303377.json")
# write.csv(data_test, file = "data/data3_test.csv")
data3_final <- combined_data %>% select(-c(shot_outcome_name,
shot_saved_off_target,
shot_saved_to_post)) %>%
mutate(is_head = ifelse(shot_body_part_name == "Head", 1, 0),
is_right_foot = ifelse(shot_body_part_name == "Right Foot", 1, 0),
is_left_foot = ifelse(shot_body_part_name == "Left Foot", 1, 0),
is_open_play = ifelse(shot_type_name == "Open Play", 1, 0)) %>%
select(-shot_body_part_name)
write_csv(data3_final, file = "data/final_data.csv")
##################### The fourth dataset ############################## ##################### The fourth dataset ##############################