Skip to content

Instantly share code, notes, and snippets.

@dwillis
Created September 6, 2021 14:52
Show Gist options
  • Save dwillis/4df61bfa43662dd825715e4759b4c5c0 to your computer and use it in GitHub Desktop.
Save dwillis/4df61bfa43662dd825715e4759b4c5c0 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(stringr)
library(fs)
results <- data.frame()
years <- c('2021','2020')
for (year in years) {
data_dir <- str_c("/Users/dwillis/code/wbb/fiba/", year)
csv_files <- fs::dir_ls(data_dir, regexp = "\\.csv$")
results <- csv_files %>%
map_dfr(~read_csv(.x, col_types=cols(minutes = col_time(format = "%M:%S"), number=col_character(), date=col_character()), col_names = c('team1','team2','team1_score','team2_score','date','number','player','minutes','points','fg','fg2','fg3','ft','offensive_rebounds','defensive_rebounds','rebounds','assists','fouls','turnovers','steals','blocks','plus_minus','efficiency'), skip = 1), .id = "source") %>%
mutate(year=year) %>%
mutate(source = str_replace(source, data_dir, "")) %>%
mutate(source = str_replace(source, replace_str, "")) %>%
mutate(source = str_replace(source, "/", "")) %>%
mutate(tmp_chunks = stringr::str_split(fg,"/",n=2)) %>%
mutate(fgm=map_chr(tmp_chunks, 1), fga=map_chr(tmp_chunks, 2)) %>%
select(-tmp_chunks) %>%
mutate(tmp_chunks = stringr::str_split(fg2,"/",n=2)) %>%
mutate(fg2m=map_chr(tmp_chunks, 1), fg2a=map_chr(tmp_chunks, 2)) %>%
select(-tmp_chunks) %>%
mutate(tmp_chunks = stringr::str_split(fg3,"/",n=2)) %>%
mutate(fg3m=map_chr(tmp_chunks, 1), fg3a=map_chr(tmp_chunks, 2)) %>%
select(-tmp_chunks) %>%
mutate(tmp_chunks = stringr::str_split(ft,"/",n=2)) %>%
mutate(ftm=map_chr(tmp_chunks, 1), fta=map_chr(tmp_chunks, 2)) %>%
select(-tmp_chunks) %>%
select(-fg,-fg2,-fg3,-ft) %>%
mutate(char_date=paste(str_squish(date), str_squish(as.character(year))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment