我正在使用我在杜克體育分析網站上找到的功能從 sportsreference.com 上抓取大學籃球比賽。它需要一個 url 作為引數。下面的代碼為杜克從 2011 年到 2022 年抓取了所有游戲。我有一個多支球隊的名單(杜克、貝勒、堪薩斯、岡薩加)。我需要在與我列出的 4 個團隊名稱連接的 url 中替換“duke”。我如何更改此代碼以回圈遍歷所有這些團隊,而不僅僅是硬編碼到代碼中的 Duke?
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)))
uj5u.com熱心網友回復:
我們可能需要創建組合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"))
-輸出
> 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438470.html
下一篇:如何在回圈定義中使用引數
