Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Keisuke ANDO
socceR
Commits
3e61c0b5
Commit
3e61c0b5
authored
Jan 23, 2024
by
Masaki Ban
🍜
Browse files
サードオブザピッチを算出プログラムの実装
parent
cc25f648
Changes
1
Show whitespace changes
Inline
Side-by-side
R/get_third.R
0 → 100644
View file @
3e61c0b5
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"
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment