engsoccerdata icon indicating copy to clipboard operation
engsoccerdata copied to clipboard

2021 england_current()

Open jalapic opened this issue 2 years ago • 1 comments

need to update :(

england_current <- function(){
  
  
  home<-visitor<-hgoal<-vgoal<-goaldif<-FT<-Season<-division<-result<-NULL
  
  url1 <- "https://www.11v11.com/competitions/premier-league/2022/matches/"
  url2 <- "https://www.11v11.com/competitions/league-championship/2022/matches/"
  url3 <- "https://www.11v11.com/competitions/league-one/2022/matches/"
  url4 <- "https://www.11v11.com/competitions/league-two/2022/matches/"
  
  x1 <- xml2::read_html(url1) %>% rvest::html_table(fill = TRUE)
  x2 <- xml2::read_html(url2) %>% rvest::html_table(fill = TRUE)
  x3 <- xml2::read_html(url3) %>% rvest::html_table(fill = TRUE)
  x4 <- xml2::read_html(url4) %>% rvest::html_table(fill = TRUE)
  
  make_data <- function(x){
    x <- x[[1]][,1:4]
    x <- as.data.frame(x)
    x <-x[grepl("([0-9]+).*$", x[,1]),]#get rid of months text
    x <-x[grepl("([0-9]+).*$", x[,3]),]#get rid of blank scores
    colnames(x)<-c("Date","home","FT","visitor")
    x$Date <- as.character(as.Date(x$Date, format="%d %b %Y"))
    x$Season <- 2021
    x$FT <- gsub(":", "-", x$FT)
    x <- x[nchar(x$FT)>1,]
    hgvg <- matrix(unlist(strsplit(x$FT, "-")), ncol=2, byrow = T)
    x$hgoal <- as.numeric(hgvg[,1])
    x$vgoal <- as.numeric(hgvg[,2])
    x$totgoal <- x$hgoal+x$vgoal
    x$goaldif <- x$hgoal-x$vgoal
    x$result <- ifelse(x$hgoal>x$vgoal, "H", ifelse(x$hgoal<x$vgoal, "A", "D"))
    return(x)
  }
  
  x1d <- make_data(x1)
  x2d <- make_data(x2)
  x3d <- make_data(x3)
  x4d <- make_data(x4)
  
  x1d$division <- 1
  x1d$tier <- 1
  x2d$division <- 2
  x2d$tier <- 2
  x3d$division <- 3
  x3d$tier <- 3
  x4d$division <- 4
  x4d$tier <- 4
  
  xd <- rbind(x1d,x2d,x3d,x4d)
  xd <- xd[colnames(engsoccerdata::england)]
  
  xd %>%
    dplyr::mutate(home = dplyr::case_when(
      grepl("Brighton and Hove", home) ~ "Brighton & Hove Albion",
      grepl("Cheltenham Town", home) ~ "Cheltenham",
      grepl("Stevenage", home) ~ "Stevenage Borough",
      grepl("Harrogate Town", home) ~ "Harrogate Town A.F.C.",
      grepl("Macclesfield Town", home) ~ "Macclesfield",
      grepl("Yeovil", home) ~ "Yeovil",
      TRUE ~ home
    )) %>%
    dplyr::mutate(visitor = dplyr::case_when(
      grepl("Brighton and Hove", visitor) ~ "Brighton & Hove Albion",
      grepl("Cheltenham Town", visitor) ~ "Cheltenham",
      grepl("Stevenage", visitor) ~ "Stevenage Borough",
      grepl("Macclesfield Town", visitor) ~ "Macclesfield",
      grepl("Harrogate Town", visitor) ~ "Harrogate Town A.F.C.",
      grepl("Yeovil", visitor) ~ "Yeovil",
      TRUE ~ visitor
    )) -> xd
  
  return(xd)
  
}


jalapic avatar Oct 07 '21 00:10 jalapic

the above code should work for 2021-2022 season.

jalapic avatar Oct 07 '21 00:10 jalapic