Home > Enterprise >  R: Loop Output is Shorter Than Expected?
R: Loop Output is Shorter Than Expected?

Time:09-16

I am working with the R programming language.

I have a vector of names that looks something like this:

all_names = list( "name1", "name2" ,  "name3" ,  "name4"  ,  "name5"  )
 

I then have these 3 objects:

a = "https://www.website.ca/"

b = "/ext1/"

c = ".com"

I am trying to produce the following result:

I thought I could do this with the following code (using a double loop):

all_urls = list()

for (j in 1:5){

for (i in 1:length(all_names))

{
 n_i = all_names[j]
 url_i = paste0(a,i,b,n_i ,c)
 all_urls[[i]] = url_i
}
}

This is not exactly working the way I had intended - could someone please show me how to fix this?

Thanks!

CodePudding user response:

One option is to store the results in a matrix, e.g.

all_names = list( "name1", "name2" ,  "name3" ,  "name4"  ,  "name5"  )

a = "https://www.website.ca/"
b = "/ext1/"
d = ".com"

all_urls = matrix(1:25, nrow = 5)
for (j in 1:5) {
  for (i in seq_along(all_names)) {
    all_urls[i,j] = paste0(a,i,b,all_names[j],d)
  }
}
all_urls
#>      [,1]                                     
#> [1,] "https://www.website.ca/1/ext1/name1.com"
#> [2,] "https://www.website.ca/2/ext1/name1.com"
#> [3,] "https://www.website.ca/3/ext1/name1.com"
#> [4,] "https://www.website.ca/4/ext1/name1.com"
#> [5,] "https://www.website.ca/5/ext1/name1.com"
#>      [,2]                                     
#> [1,] "https://www.website.ca/1/ext1/name2.com"
#> [2,] "https://www.website.ca/2/ext1/name2.com"
#> [3,] "https://www.website.ca/3/ext1/name2.com"
#> [4,] "https://www.website.ca/4/ext1/name2.com"
#> [5,] "https://www.website.ca/5/ext1/name2.com"
#>      [,3]                                     
#> [1,] "https://www.website.ca/1/ext1/name3.com"
#> [2,] "https://www.website.ca/2/ext1/name3.com"
#> [3,] "https://www.website.ca/3/ext1/name3.com"
#> [4,] "https://www.website.ca/4/ext1/name3.com"
#> [5,] "https://www.website.ca/5/ext1/name3.com"
#>      [,4]                                     
#> [1,] "https://www.website.ca/1/ext1/name4.com"
#> [2,] "https://www.website.ca/2/ext1/name4.com"
#> [3,] "https://www.website.ca/3/ext1/name4.com"
#> [4,] "https://www.website.ca/4/ext1/name4.com"
#> [5,] "https://www.website.ca/5/ext1/name4.com"
#>      [,5]                                     
#> [1,] "https://www.website.ca/1/ext1/name5.com"
#> [2,] "https://www.website.ca/2/ext1/name5.com"
#> [3,] "https://www.website.ca/3/ext1/name5.com"
#> [4,] "https://www.website.ca/4/ext1/name5.com"
#> [5,] "https://www.website.ca/5/ext1/name5.com"

Created on 2022-09-16 by the reprex package (v2.0.1)


Or, if you want to store the results in a list, one option is to initialise an empty list inside the first loop, then add the results of the inner loop to the outer list:

all_names = list( "name1", "name2" ,  "name3" ,  "name4"  ,  "name5"  )

a = "https://www.website.ca/"
b = "/ext1/"
d = ".com"

all_urls = list()

for (j in 1:5) {
  result_nested <- list()
  for (i in seq_along(all_names)) {
    result_nested[[i]] = paste0(a,i,b,all_names[j],d)
  }
  all_urls[[j]] <- result_nested
}
all_urls
#> [[1]]
#> [[1]][[1]]
#> [1] "https://www.website.ca/1/ext1/name1.com"
#> 
#> [[1]][[2]]
#> [1] "https://www.website.ca/2/ext1/name1.com"
#> 
#> [[1]][[3]]
#> [1] "https://www.website.ca/3/ext1/name1.com"
#> 
#> [[1]][[4]]
#> [1] "https://www.website.ca/4/ext1/name1.com"
#> 
#> [[1]][[5]]
#> [1] "https://www.website.ca/5/ext1/name1.com"
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [1] "https://www.website.ca/1/ext1/name2.com"
#> 
#> [[2]][[2]]
#> [1] "https://www.website.ca/2/ext1/name2.com"
#> 
#> [[2]][[3]]
#> [1] "https://www.website.ca/3/ext1/name2.com"
#> 
#> [[2]][[4]]
#> [1] "https://www.website.ca/4/ext1/name2.com"
#> 
#> [[2]][[5]]
#> [1] "https://www.website.ca/5/ext1/name2.com"
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> [1] "https://www.website.ca/1/ext1/name3.com"
#> 
#> [[3]][[2]]
#> [1] "https://www.website.ca/2/ext1/name3.com"
#> 
#> [[3]][[3]]
#> [1] "https://www.website.ca/3/ext1/name3.com"
#> 
#> [[3]][[4]]
#> [1] "https://www.website.ca/4/ext1/name3.com"
#> 
#> [[3]][[5]]
#> [1] "https://www.website.ca/5/ext1/name3.com"
#> 
#> 
#> [[4]]
#> [[4]][[1]]
#> [1] "https://www.website.ca/1/ext1/name4.com"
#> 
#> [[4]][[2]]
#> [1] "https://www.website.ca/2/ext1/name4.com"
#> 
#> [[4]][[3]]
#> [1] "https://www.website.ca/3/ext1/name4.com"
#> 
#> [[4]][[4]]
#> [1] "https://www.website.ca/4/ext1/name4.com"
#> 
#> [[4]][[5]]
#> [1] "https://www.website.ca/5/ext1/name4.com"
#> 
#> 
#> [[5]]
#> [[5]][[1]]
#> [1] "https://www.website.ca/1/ext1/name5.com"
#> 
#> [[5]][[2]]
#> [1] "https://www.website.ca/2/ext1/name5.com"
#> 
#> [[5]][[3]]
#> [1] "https://www.website.ca/3/ext1/name5.com"
#> 
#> [[5]][[4]]
#> [1] "https://www.website.ca/4/ext1/name5.com"
#> 
#> [[5]][[5]]
#> [1] "https://www.website.ca/5/ext1/name5.com"

Created on 2022-09-16 by the reprex package (v2.0.1)


Or, a 'tidyverse' option would be:

library(tidyverse)

all_names = list( "name1", "name2" ,  "name3" ,  "name4"  ,  "name5"  )

a = "https://www.website.ca/"
b = "/ext1/"
d = ".com"

expand.grid(a,seq_along(all_names),b,all_names,d) %>% unite("urls", 1:5, sep = "")
#>                                       urls
#> 1  https://www.website.ca/1/ext1/name1.com
#> 2  https://www.website.ca/2/ext1/name1.com
#> 3  https://www.website.ca/3/ext1/name1.com
#> 4  https://www.website.ca/4/ext1/name1.com
#> 5  https://www.website.ca/5/ext1/name1.com
#> 6  https://www.website.ca/1/ext1/name2.com
#> 7  https://www.website.ca/2/ext1/name2.com
#> 8  https://www.website.ca/3/ext1/name2.com
#> 9  https://www.website.ca/4/ext1/name2.com
#> 10 https://www.website.ca/5/ext1/name2.com
#> 11 https://www.website.ca/1/ext1/name3.com
#> 12 https://www.website.ca/2/ext1/name3.com
#> 13 https://www.website.ca/3/ext1/name3.com
#> 14 https://www.website.ca/4/ext1/name3.com
#> 15 https://www.website.ca/5/ext1/name3.com
#> 16 https://www.website.ca/1/ext1/name4.com
#> 17 https://www.website.ca/2/ext1/name4.com
#> 18 https://www.website.ca/3/ext1/name4.com
#> 19 https://www.website.ca/4/ext1/name4.com
#> 20 https://www.website.ca/5/ext1/name4.com
#> 21 https://www.website.ca/1/ext1/name5.com
#> 22 https://www.website.ca/2/ext1/name5.com
#> 23 https://www.website.ca/3/ext1/name5.com
#> 24 https://www.website.ca/4/ext1/name5.com
#> 25 https://www.website.ca/5/ext1/name5.com

Created on 2022-09-16 by the reprex package (v2.0.1)

  • Related