title: Need Help Filling List With For Loop in R
body: <p>I have a dataset of varying range raw inputs. I'd like to store those as a list in R. The data is 4-Dimensional. </p>
  
  <p>I created a for loop to store the data in a nested list, but the data seems to only fall into the last list. I can't seem to find a problem with my loop. </p>
  
  <p>I have two different lists. In one of those, I store the data stacked together, that works fine so I don't seem to have a problem with data acquision. As for the nested loop, it skips the first layers of lists and stores the last acquired data properly. I attachted the code and I'd appreciate any help. Thanks.</p>
  
  <pre><code>mask1 = list()
  mask2 = list()
  mask3 = list()
  mask4 = list()
  mask5 = list()
  
  mask1$vid = list()
  mask2$vid = list()
  mask3$vid = list()
  mask4$vid = list()
  mask5$vid = list()
  
  mask1$all = list()
  mask2$all = list()
  mask3$all = list()
  mask4$all = list()
  mask5$all = list()
  
  for (i in 1:6766){
    f &lt;- i%%199
    v &lt;- ceiling(i/199)
    if (f==0){
      f=199;
    }
  
    mask1$vid[[v]] = list()
    mask2$vid[[v]] = list()
    mask3$vid[[v]] = list()
    mask4$vid[[v]] = list()
    mask5$vid[[v]] = list()
  
    mask1$vid[[v]][[f]] = list()
    mask2$vid[[v]][[f]] = list()
    mask3$vid[[v]][[f]] = list()
    mask4$vid[[v]][[f]] = list()
    mask5$vid[[v]][[f]] = list()
  
    data1 = read.table(mask1_dir[i])
    data2 = read.table(mask2_dir[i])
    data3 = read.table(mask3_dir[i])
    data4 = read.table(mask4_dir[i])
    data5 = read.table(mask5_dir[i])
  
    data1=t(data1)
    data2=t(data2)
    data3=t(data3)
    data4=t(data4)
    data5=t(data5)
  
  
    for (j in 1:8) {
      m1 &lt;- data1[,j]
      m2 &lt;- data2[,j]
      m3 &lt;- data3[,j]
      m4 &lt;- data4[,j]
      m5 &lt;- data5[,j]
  
      m1&lt;-m1[m1&gt;0]
      m2&lt;-m2[m2&gt;0]
      m3&lt;-m3[m3&gt;0]
      m4&lt;-m4[m4&gt;0]
      m5&lt;-m5[m5&gt;0]
  
  
      mask1$vid[[v]][[f]][[j]] = t(data.frame(m1))
      mask2$vid[[v]][[f]][[j]] = t(data.frame(m2))
      mask3$vid[[v]][[f]][[j]] = t(data.frame(m3))
      mask4$vid[[v]][[f]][[j]] = t(data.frame(m4))
      mask5$vid[[v]][[f]][[j]] = t(data.frame(m5))
  
      mask1$vid[[v]][[f]][[j]]
  
      if (v==1 &amp; f==1){
        mask1$all[[j]] = t(data.frame(m1))
        mask2$all[[j]] = t(data.frame(m2))
        mask3$all[[j]] = t(data.frame(m3))
        mask4$all[[j]] = t(data.frame(m4))
        mask5$all[[j]] = t(data.frame(m5))
  
      }
        else{
          for (k in 1:length(m1)){
            mask1$all[[j]][(length(mask1$all[[j]])+1)] = m1[k]}
          for (k in 1:length(m2)){
            mask2$all[[j]][(length(mask2$all[[j]])+1)] = m2[k]}
          for (k in 1:length(m3)){
            mask3$all[[j]][(length(mask3$all[[j]])+1)] = m3[k]}
          for (k in 1:length(m4)){
            mask4$all[[j]][(length(mask4$all[[j]])+1)] = m4[k]}
          for (k in 1:length(m5)){
            mask5$all[[j]][(length(mask5$all[[j]])+1)] = m5[k]}
      }
    }
  
  }
  </code></pre>