Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Loop Output is Shorter Than Expected?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading