Commit dfe24779 authored by Masaki Ban's avatar Masaki Ban 🍜
Browse files

csv形式のrcgファイルのパーサを実装

parent 24762dbd
library(tidyverse)
source("R/make_data.R")
source("R/read_rcl.R")
source("R/read_rcg.R")
path_rcg <- "data/json/20221005141342-Alice2021_0-vs-HELIOS2021_1.rcg"
path_rcl <- "data/json/20221005141342-Alice2021_0-vs-HELIOS2021_1.rcl"
rcg <- read_rcg(path_rcg)
rcl <- read_rcl(path_rcl)
path_rcg <- "data/sep/20231218134000-HELIOS_base_6-vs-HELIOS_enemy_4.rcg"
path_csv <-"data/sep/20231218134000-HELIOS_base_6-vs-HELIOS_enemy_4.csv"
path_rcl <- "data/sep/20231218134000-HELIOS_base_6-vs-HELIOS_enemy_4.rcl"
rcg <- read_rcg_csv(path_csv)
rcl <- read_rcl(path_rcl)
name <- get_name(path_rcg)
ball <- get_ball(rcg)
referee <- get_referee(rcl)
goal <- read_goal(referee,name)
players <- get_player(rcg,name)
action <- get_action(rcl,ball,players)
kick <- get_kick_log(action,goal)
pass <- get_pass(kick)
...@@ -10,11 +10,33 @@ read_rcg <- function(path) { ...@@ -10,11 +10,33 @@ read_rcg <- function(path) {
jsonlite::parse_json(simplifyVector = TRUE, flatten = TRUE) |> jsonlite::parse_json(simplifyVector = TRUE, flatten = TRUE) |>
tibble::as_tibble() |> tibble::as_tibble() |>
dplyr::filter(type == "show") |> dplyr::filter(type == "show") |>
dplyr::select(time, stime, players, ball.x, ball.y, ball.vx, ball.vy) |> dplyr::select(time, players, ball.x, ball.y, ball.vx, ball.vy) |>
tidyr::unnest(players) |> tidyr::unnest(players) |>
dplyr::select(time:capacity, ball.x:ball.vy) |> dplyr::select(time:unum, x:neck, ball.x:ball.vy) |>
dplyr::rename(step = time) |> dplyr::rename(step = time) |>
dplyr::rename_with(stringr::str_replace, pattern = "\\.", replacement = "_") dplyr::rename_with(stringr::str_replace, pattern = "\\.", replacement = "_")
return(rcg)
}
read_rcg_csv <- function(path) {
rcg <- path |>
readr::read_csv() |>
tibble::as_tibble() |>
dplyr::filter(stopped == 0) |>
dplyr::select(-"#", -stopped:-r_pen_score, -matches("^[lr]\\d+_t")) |>
pivot_longer(
cols = -c(cycle, b_x, b_y, b_vx, b_vy), # ボールのデータ列を除外
names_to = c("side", "unum", ".value"), # 新しい列名を設定
names_pattern = "([lr])(\\d+)_(.+)" # 正規表現で元の列名を分割
) |>
mutate(unum = as.numeric(unum)) |>
dplyr::rename(
step = cycle,
ball_x = b_x,
ball_y = b_y,
ball_vx = b_vx,
ball_vy = b_vy
) |>
select(-ball_x, -ball_y, -ball_vx, -ball_vy, everything(), ball_x, ball_y, ball_vx, ball_vy)
return(rcg) return(rcg)
} }
\ No newline at end of file
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