Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2021 england_current() #73

Open
jalapic opened this issue Oct 7, 2021 · 1 comment
Open

2021 england_current() #73

jalapic opened this issue Oct 7, 2021 · 1 comment

Comments

@jalapic
Copy link
Owner

jalapic commented Oct 7, 2021

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
Copy link
Owner Author

jalapic commented Oct 7, 2021

the above code should work for 2021-2022 season.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant