R/player_table.R

#SUFC Reports
#Player Table Statistics

player_table <- function(match_report){
  library(pdftools, quietly = TRUE)
  library(dplyr, quietly = TRUE)
  
  test <- match_report
  file <- pdf_text(test)
  file_page_2 <- file[2]
  keyword <- "HEAD TO HEAD PLAYER STATS"
  
  file_page_2_sub <- sub(paste0(".*",keyword), "", file_page_2)
  file_page_2_sub <- gsub(pattern = "^ *|(?<= ) | *$", replacement = "|", x = file_page_2_sub, perl = TRUE)
  file_page_2_sub <- gsub("([[:punct:]])\\1+", "\\1", file_page_2_sub)
  file_page_2_sub <- strsplit(file_page_2_sub, "\n")
  team_stats <- file_page_2_sub
  
  team_stats <- team_stats[[1]][2:(length(team_stats[[1]]) - 2)]
  team_stats[2] <- c("Position (H) |Name (H) |Tackles Made (H) |Tackles Slowed (H) |Tackles Missed (H) |Hitups (H) |Linebreaks (H) |Handling Errors (H) |Penalties (H) |Kicks In Play (H) |Points (H) |Position (H) |Name (A) |Tackles Made (A) |Tackles Slowed (A) |Tackles Missed (A) |Hitups (A) |Linebreaks (A) |Handling Errors (A) |Penalties (A) |Kicks In Play (A) |Points (A)")
  team_stats <- team_stats[-c(3:8)]
  
  #This sub-routine merges rows that were accidently split
  for(i in 3:length(team_stats)){
    if(substr(team_stats[i], 1, 1) %in% c(0:9)){
      
    } else {
      team_stats[i] <- paste0(team_stats[i - 1], team_stats[i])
      team_stats <- print(team_stats[-(i-1)])
    }
  }
  team_stats[1] <- substr(team_stats[1], 3, nchar(team_stats[1]))
  team_stats[1] <- gsub(" |", " - ", team_stats[1], fixed = TRUE)
  
  team_stats
}
AndrewFerris/sufc1863 documentation built on May 5, 2019, 5:59 a.m.