Storing the output of a "for loop" in a list of numeric vectors

My problem is related to this loop:

for (i in 1:60){
for (j in 1:10){
for (k in 1:6){
PC12rotlist[i]<-Map("+", PC1rotlist[j], PC2rotlist[k])
}
}
}

where:
PC1rotlist is a list of 10 numeric vectors (double) [1:120]
PC2rotlist is a list of 6 numeric vectors (double) [1:120]
PC12rotlist is a list of 60 numeric vectors (double) [1:120]

As you can see, I am trying to get the sum of all possible combinations of vectors contained in two lists (PC1rotlist with 10 vectors and PC2rotlist with 6 vectors). The output should be then a list of 60 numeric vectors, containing the sums of all the possible combinations. However, I get a list where all the 60 vectors are the same, corresponding to the result of the last combination:
Map("+", PC1rotlist[10], PC2rotlist[6])

Could someone help me with this issue and let me know how I could store this ouput properly? I am not very familiarized with loops and I have been a couple of days trying to solve it without success.

Instead of looping on i, you can just increment i within the inner loop. Try running this code.

i <- 1
for (j in 1:10) {
  for (k in 1:6) {
    print(i)
    i <- i + 1
  }
}

Thank you very much. I got to store the ouput properly following your recommendation. Here is the code that is working:

i <- 1
for (j in 1:6) {
for (k in 1:10) {
PC12rotlist[i]<-Map("+", PC1rotlist[k], PC2rotlist[j])
print(i)
i <- i + 1
}
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.