Question

I am new to R, i have many list of different sized lists, how can i save them in a data structure so that later on i could iterate over them. I think it is doable using data-frames if lists are of same size.
Assume you have 10 of such [ [ [ ],[ ],[ ] ] , [ [ ],[ ],[ ],[ ] ] , [ [ ],[ ] ] ] how to put all 10 in a single referenced variable

i hope this sample code clarifies it:

max=sample(1:10,6,replace=F)

for (n in 1:5){
  neighborList=list()

  for ( i in 1:max[n]){
    num=sample(15:49,20,replace=F) 
    neighborList[i] = 0

    for (j in 1:num[i])
    {
      neighborList[[i]][j] = i*j
    }
  }
}

each iteration of n, the sample code will create a different list of lists, i want to save by append them after each iteration, and use it for later. For example if variable temp saves all the list of lists generated , if possible i could say x[1] it should give me the first neighborList that was generated when n=1.

Any help is appreciated.

Was it helpful?

Solution

max=sample(1:10,6,replace=F)

temp <- list()
for (n in 1:5){
  neighborList=list()

  for ( i in 1:max[n]){
    num=sample(15:49,20,replace=F) 
    neighborList[i] = 0

    for (j in 1:num[i])
    {
      neighborList[[i]][j] = i*j
    }
  }
  temp[[n]] <- neighborList
}

Doesn't temp[[1]] or temp[1] already give you what you want, i.e. the first neighborList that was generated when n=1?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top