title: Iterate through a nested list
body: <p>I am attempting to iterate through a nested list in R, and can't quite get the function/for loop correct.</p>
  <p>Sample of my data:</p>
  <pre><code>&gt; str(waveforms)
  List of 3
  $ Sta2_Ev20:List of 7
  ..$ 1: num [1:10000] 5.88e-05 -2.84e-05 -5.50e-05 7.02e-05 1.90e-06 ...
  ..$ 2: num [1:10000] 2.61e-05 -2.14e-05 -2.02e-05 2.97e-05 5.94e-06 ...
  ..$ 3: num [1:10000] 1.08e-05 -4.12e-05 1.95e-05 3.03e-05 -4.55e-05 ...
  ..$ 4: num [1:10000] 2.45e-05 -1.23e-05 -1.53e-05 2.76e-05 3.07e-06 ...
  ..$ 5: num [1:10000] 2.29e-05 0.00 5.71e-06 -2.86e-05 5.71e-06 ...
  ..$ 6: num [1:10000] -1.01e-04 2.37e-05 2.08e-05 -5.93e-06 2.08e-05 ...
  ..$ 7: num [1:10000] 3.47e-05 -2.75e-05 0.00 1.45e-05 -1.45e-06 ...
  $ Sta2_Ev21:List of 34
  ..$ 1 : num [1:10000] 1.35e-05 -3.46e-05 -3.46e-05 8.65e-05 -2.11e-05 ...
  ..$ 2 : num [1:10000] 5.68e-05 1.14e-05 -7.38e-05 2.27e-05 4.73e-05 ...
  ..$ 3 : num [1:10000] 8.21e-06 3.69e-05 -2.46e-05 1.64e-05 -8.21e-06 ...
  ..$ 4 : num [1:10000] 3.26e-05 -1.34e-05 -1.19e-05 8.90e-06 1.78e-05 ...
  ..$ 5 : num [1:10000] 2.43e-05 -3.00e-05 1.29e-05 2.86e-06 -1.00e-05 ...
  ..$ 6 : num [1:10000] -6.87e-06 2.34e-05 -2.34e-05 3.44e-05 -2.20e-05 ...
  ..$ 7 : num [1:10000] 1.23e-05 -5.75e-05 2.46e-05 1.23e-05 -2.74e-06 ...
  ..$ 8 : num [1:10000] -2.34e-05 -2.17e-05 1.83e-05 4.17e-05 -4.50e-05 ...
  ..$ 9 : num [1:10000] 3.34e-05 7.42e-06 -2.04e-05 7.42e-06 0.00 ...
  etc...
  </code></pre>
  <p>REPRODUCIBLE DATA</p>
  <pre><code>Sta2_Evt1=list(a=runif(10000, min=-12, max=12), b=runif(10000, min=-12, max=12),c=runif(10000, min=-12, max=12)) 
  Sta2_Evt2=list(a=runif(10000, min=-2, max=2), b=runif(10000, min=-2, max=2),c=runif(10000, min=-2, max=2))  
  ...  
  waveforms=list(Sta2_Evt1,Sta2_Evt2,...))
  binsize=5000
  </code></pre>
  <p>And so on.  What I need to do it iterate through each list within my list.  I tested the data on one of the &quot;Sta#_Evt#&quot; lists.  Previously, this code worked:</p>
  <pre><code>ch0=list()
  for (i in seq_along(Sta2_Evt2)) {
    tempobj=head(Sta2_Evt2[[i]],n=binsize)
    name &lt;- paste('click',names(Sta2_Evt2)[[i]],sep='')
    ch0[[name]] &lt;- tempobj
  }
  </code></pre>
  <p>This is simple, just extracting the first 5000 data points from each element. From this new list of elements (ch0), I was able to run multiple scripts to process my data.  However, now that I need to expand to include ALL my data, not just the test set I was originally working with, I can't figure out how to run iterations over nested lists (like waveform, above). When I run the code for 'ch0', for instance, over my nested 'waveform' list, it returns the same nested list.</p>
  <p>I have tried a few methods: lapply, an additional for loop, llply. I think that maybe writing a function to complete my analysis, and then using llply. However, with this function:</p>
  <pre><code>mkChs=function(x,binsize) {for (i in 1:length(x)) {
    head(x[[i]],n=binsize)
  }}
  test=llply(waveforms,mkChs, binsize=5000)
  </code></pre>
  <p>It still does not work.  The new list 'test' comes back empty.</p>
  <p>I've tried a nest for loop.</p>
  <pre><code>ch0=list()
  for (i in seq_along(waveforms)) {
    a=list(names(waveforms)[[i]])
    b=for (j in seq_along(waveforms[i])) {
      tempobj=head(waveforms[[i]][[j]],n=binsize)
      name &lt;- paste('click',seq_along(waveforms)[[i]][[j]]-1,sep='')
     a[[name]] &lt;- tempobj
    }
  name1 &lt;- names(waveforms)[[i]]
  ch0[[name1]] &lt;- b
  }
  </code></pre>
  <p>That returns the following:</p>
  <pre><code>str(ch0)
  List of 3
  $ Sta2_Ev20: num [1:5000] 5.88e-05 -2.84e-05 -5.50e-05 7.02e-05 1.90e-06 ...
  $ Sta2_Ev21: num [1:5000] 1.35e-05 -3.46e-05 -3.46e-05 8.65e-05 -2.11e-05 ...
  $ Sta2_Ev22: num [1:5000] 2.06e-05 3.44e-06 2.06e-05 -3.44e-05 0.00 ...
  </code></pre>
  <p>Not exactly what I am looking for. I'd rather not have a separate list per &quot;Sta#_Evt#&quot; to get this to run properly.</p>