Commit 3e61c0b5 authored by Masaki Ban's avatar Masaki Ban 🍜
Browse files

サードオブザピッチを算出プログラムの実装

parent cc25f648
library(tidyverse)
# CSVファイルのリストを作成
file_list <- list.files(path = "/home/banma/Desktop/logs/base2base30half/pass_data/",
pattern = "*.csv", full.names = TRUE)
# 各ファイルからデータを読み込み、処理して結果をリストに保存
results_list <- lapply(file_list, function(file) {
df <- read_csv(file)
count_df <- df %>%
mutate(category = case_when(
ball_x <= -17.5 ~ "defence",
ball_x > -17.5 & ball_x <= 17.5 ~ "middle",
ball_x > 17.5 ~ "attack"
)) %>%
count(category) %>%
pivot_wider(names_from = category, values_from = n, values_fill = list(n = 0))
# ファイル名を最初の列に追加
count_df <- count_df %>%
mutate(file = basename(file)) %>%
select(file, everything())
return(count_df)
})
# 全ての結果を縦方向に結合
pass_df <- bind_rows(results_list)
# NAを0に変換
pass_df[is.na(pass_df)] <- 0
# 各カテゴリの割合を計算
pass_df <- pass_df %>%
mutate(defence_rate = defence / (defence + middle + attack),
middle_rate = middle / (defence + middle + attack),
attack_rate = attack / (defence + middle + attack))
# 各カテゴリの割合の箱ひげ図を作成
p <- pass_df %>%
pivot_longer(cols = c(defence_rate, middle_rate, attack_rate), names_to = "category", values_to = "rate") %>%
ggplot(aes(x = factor(category, levels = c("defence_rate", "middle_rate", "attack_rate")), y = rate)) +
geom_boxplot() +
stat_summary(fun = mean, geom = "point", shape = 20, size = 3, color = "red") +
scale_x_discrete(labels = c("ディフェンシブサード", "ミドルサード", "アタッキングサード")) +
scale_y_continuous(limits = c(0, 0.8)) +
labs(x = "サードオブザピッチ", y = "割合")
# 結果を保存
ggsave("third.pdf", p, device = cairo_pdf, width = 4, height = 3)
# 結果をCSVファイルとして保存
write_csv(pass_df, "third.csv")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment