get_third.R 1.92 KB
Newer Older
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
49
50
51
52
53
54
55
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")