适用于R中不同速度的循环

时间:2018-06-22 16:20:34

标签: r

我正在尝试建立一个循环,以创建具有70年和每年50页的网址。任何帮助或指导将不胜感激。

    yrs <- 1919
    pgs <- 0
urltest <- as.character()
yrsrepeat <- 0

for(i in 1:70){
    yrs[i] <- yrs + i
    pgs[i] <- pgs + i
    yrsrepeat[i] <- rep.int(yrs[i], length(pgs))
    urltest[i] <- paste0(url1, yrs[i], url2, url3, pgs[i], url4)
  }



url1 <- "https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=y&type=0&season=2017&month=0&season1=1920&ind=0&team=0&rost=0&age=0&filter=&players=0&page=1_30"
url2 <- "_30"
pages <- 1:5


url1 <- "https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=y&type=8&season="
url2 <- "&month=0&season1="
url3 <- "&ind=0&team=&rost=&age=&filter=&players=&page="
url4 <- "_30"
year <- 1920:1924
pages <- 1:5

urls <- paste0(url1, year, url2, year, url3, pages, url4)

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么我会做类似的事情:

urls = list() # to store each year's urls

for(i in 1930:1999){

    current_year_urls = paste("url",i ,"pg", 1:50,sep = "") 

    # check if first year
    if (length(urls) == 0){
        urls = current_year_urls

    }else{
       # if not first year, append it to the list
       urls = rbind(urls,current_year_urls)
    }