I am using a function I found on a Duke Sports Analytics site to scrape college basketball games from sportsreference.com. It takes a url as an argument. The code below scrapes all games for Duke from 2011-2022. I have a list of multiple teams (Duke, Baylor, Kansas, Gonzaga). I need to substitute "duke" within the url being concatenated with the 4 team names I listed. How could I alter this code to loop through all those teams, and not just Duke which is hardcoded into the code?
teams <- as.list(c("Gonzaga", "Baylor", "Kansas", "Duke"))
game_logs <- tibble(
season_url = 2011:2022,
season = paste0(season_url - 1, "-", str_sub(season_url, start = 3)),
url = paste0("https://www.sports-reference.com/cbb/schools/duke/", season_url, "-
schedule.html")
) %>%
mutate(data = map(url, possibly(scrape_game_logs, otherwise = NA)))
CodePudding user response:
We may need to create combinations with crossing
library(dplyr)
library(tidyr)
library(stringr)
teams <- c("Gonzaga", "Baylor", "Kansas", "Duke")
df1 <- crossing(season_url = 2011:2022, teams) %>%
mutate(season = str_c(season_url - 1, "-", str_sub(season_url, start = 3)),
url = glue::glue("https://www.sports-reference.com/cbb/schools/",
"{teams}/{season_url}-schedule.html"))
-output
> df1
# A tibble: 48 × 4
season_url teams season url
<int> <chr> <chr> <glue>
1 2011 Baylor 2010-11 https://www.sports-reference.com/cbb/schools/Baylor/2011-schedule.html
2 2011 Duke 2010-11 https://www.sports-reference.com/cbb/schools/Duke/2011-schedule.html
3 2011 Gonzaga 2010-11 https://www.sports-reference.com/cbb/schools/Gonzaga/2011-schedule.html
4 2011 Kansas 2010-11 https://www.sports-reference.com/cbb/schools/Kansas/2011-schedule.html
5 2012 Baylor 2011-12 https://www.sports-reference.com/cbb/schools/Baylor/2012-schedule.html
6 2012 Duke 2011-12 https://www.sports-reference.com/cbb/schools/Duke/2012-schedule.html
7 2012 Gonzaga 2011-12 https://www.sports-reference.com/cbb/schools/Gonzaga/2012-schedule.html
8 2012 Kansas 2011-12 https://www.sports-reference.com/cbb/schools/Kansas/2012-schedule.html
9 2013 Baylor 2012-13 https://www.sports-reference.com/cbb/schools/Baylor/2013-schedule.html
10 2013 Duke 2012-13 https://www.sports-reference.com/cbb/schools/Duke/2013-schedule.html
# … with 38 more rows