Создание последовательности URL с постоянными и изменяющимися разделами URL - PullRequest
1 голос
/ 28 апреля 2019

Я пытаюсь сгенерировать вектор URL-адресов, похожих по структуре на URL-адреса, выделенные в желаемом выводе ниже. Структура довольно прямолинейна, но я не могу получить право на структуру цикла. Ток для структуры цикла, который я записал, записывает циклы по вектору offset, но неправильно корректирует циклы по векторам from и to.

## seq of vectors    
offset <- seq(from = 0, to = 600, by = 300)
from <- seq(from = 1, to = 29, by = 2)
to <- seq(from = 2, to = 30, by = 2)

## url structure
url1 <- "https://url.com/&offset="
url2 <- "&from="
url3 <- "&to="
url4 <- "&order=date"

## try 1: generates 15 urls
urlSeq <- paste0(url1, offset, url2, from, url3, to, url4)

## try 2: generates a list of 599 urls
urlSeq <- list()
i <- 1
j <- 1
k <- 1
for (i in 1:length(from)) {
  for (j in 1:length(to)) {
    while (k < 600) {
      urlSeq[k] <- paste0(url1, offset[k], url2, from[i], url3, to[j], url4)
      k <- k+1
    }
    j <- j+1
  }
  i <- i+1
}

## desired output (15 x 3 vector of urls)
## truncated for illustration purposes
> [1] "https://url.com/&offset=0&from=1&to=2&order=date"     "https://url.com/&offset=300&from=1&to=2&order=date"   "https://url.com/&offset=600&from=1&to=2&order=date"  
> [4] "https://url.com/&offset=0&from=3&to=4&order=date"     "https://url.com/&offset=300&from=3&to=4&order=date"  "https://url.com/&offset=600&from=3&to=4&order=date"
> [7] "https://url.com/&offset=0&from=5&to=6&order=date"   "https://url.com/&offset=300&from=5&to=6&order=date" "https://url.com/&offset=600&from=5&to=6&order=date"

1 Ответ

1 голос
/ 29 апреля 2019

Попробуйте это:

## seq of vectors    
offset <- seq(from = 0, to = 600, by = 300)
from <- seq(from = 1, to = 29, by = 2)
to <- seq(from = 2, to = 30, by = 2)

## url structure
url1 <- "https://url.com/&offset="
url2 <- "&from="
url3 <- "&to="
url4 <- "&order=date"

# vector witk all offset values
urlSeq <- paste0(url1, offset)

# loop every element in 'from' and 'to' and paste them to the above vector
urlSeq = sapply(1:length(from), function(x){
  paste0(urlSeq, url2, from[x], url3, to[x])
})

# Convert to a vector (from matrix)
urlSeq = as.vector(urlSeq)

# Paste the date
urlSeq= paste0(urlSeq, url4)

Результаты:

 > urlSeq
 [1] "https://url.com/&offset=0&from=1&to=2&order=date"    
 [2] "https://url.com/&offset=300&from=1&to=2&order=date"  
 [3] "https://url.com/&offset=600&from=1&to=2&order=date"  
 [4] "https://url.com/&offset=0&from=3&to=4&order=date"    
 [5] "https://url.com/&offset=300&from=3&to=4&order=date"  
 [6] "https://url.com/&offset=600&from=3&to=4&order=date"  
 [7] "https://url.com/&offset=0&from=5&to=6&order=date"    
 [8] "https://url.com/&offset=300&from=5&to=6&order=date"  
 [9] "https://url.com/&offset=600&from=5&to=6&order=date"  
[10] "https://url.com/&offset=0&from=7&to=8&order=date"    
[11] "https://url.com/&offset=300&from=7&to=8&order=date"  
[12] "https://url.com/&offset=600&from=7&to=8&order=date"  
[13] "https://url.com/&offset=0&from=9&to=10&order=date"   
[14] "https://url.com/&offset=300&from=9&to=10&order=date" 
[15] "https://url.com/&offset=600&from=9&to=10&order=date" 
[16] "https://url.com/&offset=0&from=11&to=12&order=date"  
[17] "https://url.com/&offset=300&from=11&to=12&order=date"
[18] "https://url.com/&offset=600&from=11&to=12&order=date"
[19] "https://url.com/&offset=0&from=13&to=14&order=date"  
[20] "https://url.com/&offset=300&from=13&to=14&order=date"
[21] "https://url.com/&offset=600&from=13&to=14&order=date"
[22] "https://url.com/&offset=0&from=15&to=16&order=date"  
[23] "https://url.com/&offset=300&from=15&to=16&order=date"
[24] "https://url.com/&offset=600&from=15&to=16&order=date"
[25] "https://url.com/&offset=0&from=17&to=18&order=date"  
[26] "https://url.com/&offset=300&from=17&to=18&order=date"
[27] "https://url.com/&offset=600&from=17&to=18&order=date"
[28] "https://url.com/&offset=0&from=19&to=20&order=date"  
[29] "https://url.com/&offset=300&from=19&to=20&order=date"
[30] "https://url.com/&offset=600&from=19&to=20&order=date"
[31] "https://url.com/&offset=0&from=21&to=22&order=date"  
[32] "https://url.com/&offset=300&from=21&to=22&order=date"
[33] "https://url.com/&offset=600&from=21&to=22&order=date"
[34] "https://url.com/&offset=0&from=23&to=24&order=date"  
[35] "https://url.com/&offset=300&from=23&to=24&order=date"
[36] "https://url.com/&offset=600&from=23&to=24&order=date"
[37] "https://url.com/&offset=0&from=25&to=26&order=date"  
[38] "https://url.com/&offset=300&from=25&to=26&order=date"
[39] "https://url.com/&offset=600&from=25&to=26&order=date"
[40] "https://url.com/&offset=0&from=27&to=28&order=date"  
[41] "https://url.com/&offset=300&from=27&to=28&order=date"
[42] "https://url.com/&offset=600&from=27&to=28&order=date"
[43] "https://url.com/&offset=0&from=29&to=30&order=date"  
[44] "https://url.com/&offset=300&from=29&to=30&order=date"
[45] "https://url.com/&offset=600&from=29&to=30&order=date"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...