title: Merging nested lists whilst maintaining sub-sublists names
body: <p>I am trying to combine lists by name whilst also maintaining the names of the lists <em>within</em> the nested lists.</p>
  <p>The data structure looks like this:</p>
  <pre><code>Main list
   - sublist: &quot;tree&quot;
     - named num: &quot;apple&quot;:   1  2  3
     - named num: &quot;cherry&quot;:  4  5  6
   - sublist: &quot;tree&quot;
     - named num: &quot;peach&quot;:   7  8  9
     - named num: &quot;pear&quot;:   10 11 12
   - sublist: &quot;house&quot;
     - named num: &quot;door&quot;:   20 21 22
     - named num: &quot;window&quot;: 23 24 25
  </code></pre>
  <p>I want it to look like this:</p>
  <pre><code>Main list
   - sublist: &quot;tree&quot;
     - named num: &quot;apple&quot;:   1  2  3
     - named num: &quot;cherry&quot;:  4  5  6
     - named num: &quot;peach&quot;:   7  8  9
     - named num: &quot;pear&quot;:   10 11 12
   - sublist: &quot;house&quot;
     - named num: &quot;door&quot;:   20 21 22
     - named num: &quot;window&quot;: 23 24 25
  </code></pre>
  <p>(Side info: The actual data is multiple <code>csv</code> files. Each sublist represents a <code>csv</code> file and the sub-sublists <code>&quot;apple&quot;</code>, <code>&quot;cherry&quot;</code>, etc. are the names of the columns/variables inside the <code>csv</code> file.)</p>
  <p>I tried the code below and the sublists get merged successfully. However it gets rid of the names like <code>&quot;apple&quot;</code>, <code>&quot;cherry&quot;</code>, <code>&quot;pear&quot;</code>. Their structure/format is no longer <code>named num</code> but just <code>num</code>. Can somebody help?</p>
  <p>Code:</p>
  <pre><code>nm &lt;- names(MainList)
  MainList1 &lt;- lapply(unique(nm), function(n) unname(unlist(MainList[nm %in% n])))
  names(MainList1) &lt;- unique(nm)
  </code></pre>
  <p>Edit: I added the original data.
  This is what the data looks like originally. The main list is called <code>zF_10_agg1</code>, the sublists are called <code>306</code>, <code>371</code>, <code>371</code> and <code>389</code>. The <code>307</code> have different columns inside of them. I want the <code>VA01</code> and <code>VA02</code> columns of the two <code>371</code> lists inside ONE <code>371</code> list. I will have a more data later and thus more 371-like columns, but I use this smaller data set for now to make it work later more easily.</p>
  <pre><code>&gt;      zF_10_agg1
      $`306`
          VA01_F0finEnv_sma      VA01_F0final_sma  VA01_jitterLocal_sma VA01_shimmerLocal_sma      VA01_mfcc_sma.0.      VA01_mfcc_sma.1.      VA01_mfcc_sma.2. 
               -0.190604977          -0.153007721           0.108863052           0.148992969           0.082147057           0.038587473           0.273740917 
           VA01_mfcc_sma.3.      VA01_mfcc_sma.4.      VA01_mfcc_sma.5.      VA01_mfcc_sma.6.      VA01_mfcc_sma.7.      VA01_mfcc_sma.8.      VA01_mfcc_sma.9. 
                0.247582486           0.048753122           0.120012640          -0.094071270           0.187793327           0.103478133           0.311836830 
          VA01_mfcc_sma.10.     VA01_mfcc_sma.11.     VA01_mfcc_sma.12.     VA01_mfcc_sma.13.     VA01_mfcc_sma.14.  VA01_F0finEnv_sma SD   VA01_F0final_sma SD 
                0.096803725          -0.009998885           0.289684409           0.299622366           0.218961033           1.039143987           1.098620495 
       VA01_F0finEnv_sma RG   VA01_F0final_sma RG 
                5.400888104           5.351059703 
      
      $`371`
          VA01_F0finEnv_sma      VA01_F0final_sma  VA01_jitterLocal_sma VA01_shimmerLocal_sma      VA01_mfcc_sma.0.      VA01_mfcc_sma.1.      VA01_mfcc_sma.2. 
                 0.21414952            0.17037270           -0.11340110           -0.16155100           -0.09228902           -0.04335152           -0.30753726 
           VA01_mfcc_sma.3.      VA01_mfcc_sma.4.      VA01_mfcc_sma.5.      VA01_mfcc_sma.6.      VA01_mfcc_sma.7.      VA01_mfcc_sma.8.      VA01_mfcc_sma.9. 
                -0.27814928           -0.05477223           -0.13482953            0.10568541           -0.21097849           -0.11625365           -0.35033654 
          VA01_mfcc_sma.10.     VA01_mfcc_sma.11.     VA01_mfcc_sma.12.     VA01_mfcc_sma.13.     VA01_mfcc_sma.14.     VA01_F0finEnv_sma      VA01_F0final_sma 
                -0.10875522            0.01123336           -0.32544916           -0.33661406           -0.24599420            0.90763360            0.84537119 
          VA01_F0finEnv_sma      VA01_F0final_sma 
                 5.58352735            5.10071294 
      
      $`371`
          VA02_F0finEnv_sma      VA02_F0final_sma  VA02_jitterLocal_sma VA02_shimmerLocal_sma      VA02_mfcc_sma.0.      VA02_mfcc_sma.1.      VA02_mfcc_sma.2. 
               1.851272e-16         -5.009943e-17         -2.348591e-17         -2.604552e-17         -2.391990e-17         -3.890029e-17         -2.785709e-17 
           VA02_mfcc_sma.3.      VA02_mfcc_sma.4.      VA02_mfcc_sma.5.      VA02_mfcc_sma.6.      VA02_mfcc_sma.7.      VA02_mfcc_sma.8.      VA02_mfcc_sma.9. 
               2.683726e-17         -2.083230e-17          5.737901e-18         -2.277669e-17          3.112273e-17         -2.453523e-18         -5.662469e-17 
          VA02_mfcc_sma.10.     VA02_mfcc_sma.11.     VA02_mfcc_sma.12.     VA02_mfcc_sma.13.     VA02_mfcc_sma.14.     VA02_F0finEnv_sma      VA02_F0final_sma 
               2.266112e-17          1.361854e-18          1.131963e-17         -4.091183e-17          8.561561e-18          1.000000e+00          1.000000e+00 
          VA02_F0finEnv_sma      VA02_F0final_sma 
               1.124750e+01          1.369369e+01 
      
      $`389`
          VA03_F0finEnv_sma      VA03_F0final_sma  VA03_jitterLocal_sma VA03_shimmerLocal_sma      VA03_mfcc_sma.0.      VA03_mfcc_sma.1.      VA03_mfcc_sma.2. 
               1.586292e-18         -9.464618e-17         -2.369378e-17          2.853813e-17         -9.629405e-17         -5.495508e-17         -2.202477e-17 
           VA03_mfcc_sma.3.      VA03_mfcc_sma.4.      VA03_mfcc_sma.5.      VA03_mfcc_sma.6.      VA03_mfcc_sma.7.      VA03_mfcc_sma.8.      VA03_mfcc_sma.9. 
              -4.454892e-17         -7.952470e-17         -1.056807e-17         -6.211858e-17          4.154178e-18         -8.151347e-18          1.995314e-18 
          VA03_mfcc_sma.10.     VA03_mfcc_sma.11.     VA03_mfcc_sma.12.     VA03_mfcc_sma.13.     VA03_mfcc_sma.14.     VA03_F0finEnv_sma      VA03_F0final_sma 
               3.121848e-17          2.181543e-17          8.159633e-17          6.164483e-19          2.416510e-17          1.000000e+00          1.000000e+00 
          VA03_F0finEnv_sma      VA03_F0final_sma 
               3.918357e+00          7.132235e+00 
  </code></pre>
  <p><strong>Data :</strong></p>
  <pre><code>    list(`306` = c(VA01_F0finEnv_sma = -0.190604977349467, VA01_F0final_sma = -0.15300772088343, 
      VA01_jitterLocal_sma = 0.108863051649501, VA01_shimmerLocal_sma = 0.14899296872475, 
      VA01_mfcc_sma.0. = 0.0821470570401912, VA01_mfcc_sma.1. = 0.0385874726552808, 
      VA01_mfcc_sma.2. = 0.273740917400523, VA01_mfcc_sma.3. = 0.247582485577918, 
      VA01_mfcc_sma.4. = 0.048753122175165, VA01_mfcc_sma.5. = 0.120012639707355, 
      VA01_mfcc_sma.6. = -0.0940712695127309, VA01_mfcc_sma.7. = 0.187793326639453, 
      VA01_mfcc_sma.8. = 0.103478133127875, VA01_mfcc_sma.9. = 0.311836829876786, 
      VA01_mfcc_sma.10. = 0.096803725447679, VA01_mfcc_sma.11. = -0.00999888512522001, 
      VA01_mfcc_sma.12. = 0.289684409002433, VA01_mfcc_sma.13. = 0.299622366044721, 
      VA01_mfcc_sma.14. = 0.218961033004239, `VA01_F0finEnv_sma SD` = 1.03914398679305, 
      `VA01_F0final_sma SD` = 1.09862049506063, `VA01_F0finEnv_sma RG` = 5.40088810381152, 
      `VA01_F0final_sma RG` = 5.35105970310478), `371` = c(VA01_F0finEnv_sma = 0.214149520006098, 
      VA01_F0final_sma = 0.170372695167837, VA01_jitterLocal_sma = -0.113401101476438, 
      VA01_shimmerLocal_sma = -0.161550995230132, VA01_mfcc_sma.0. = -0.0922890207323388, 
      VA01_mfcc_sma.1. = -0.0433515233801915, VA01_mfcc_sma.2. = -0.307537264407489, 
      VA01_mfcc_sma.3. = -0.278149284560315, VA01_mfcc_sma.4. = -0.0547722348834566, 
      VA01_mfcc_sma.5. = -0.134829528812893, VA01_mfcc_sma.6. = 0.105685409254896, 
      VA01_mfcc_sma.7. = -0.210978491988385, VA01_mfcc_sma.8. = -0.116253654332496, 
      VA01_mfcc_sma.9. = -0.350336539062199, VA01_mfcc_sma.10. = -0.108755217127712, 
      VA01_mfcc_sma.11. = 0.0112333581977285, VA01_mfcc_sma.12. = -0.325449156567845, 
      VA01_mfcc_sma.13. = -0.336614064436232, VA01_mfcc_sma.14. = -0.245994196780729, 
      VA01_F0finEnv_sma = 0.907633595217961, VA01_F0final_sma = 0.845371190998525, 
      VA01_F0finEnv_sma = 5.58352735270808, VA01_F0final_sma = 5.10071293943663
      ), `371` = c(VA02_F0finEnv_sma = 1.85127192814681e-16, VA02_F0final_sma = -5.00994289546507e-17, 
      VA02_jitterLocal_sma = -2.34859079707077e-17, VA02_shimmerLocal_sma = -2.60455164544764e-17, 
      VA02_mfcc_sma.0. = -2.39198962485355e-17, VA02_mfcc_sma.1. = -3.89002856985193e-17, 
      VA02_mfcc_sma.2. = -2.78570899311261e-17, VA02_mfcc_sma.3. = 2.68372615000544e-17, 
      VA02_mfcc_sma.4. = -2.08322992987213e-17, VA02_mfcc_sma.5. = 5.73790146364059e-18, 
      VA02_mfcc_sma.6. = -2.27766888801672e-17, VA02_mfcc_sma.7. = 3.11227273727354e-17, 
      VA02_mfcc_sma.8. = -2.45352291763184e-18, VA02_mfcc_sma.9. = -5.66246851843269e-17, 
      VA02_mfcc_sma.10. = 2.26611187363704e-17, VA02_mfcc_sma.11. = 1.36185358636217e-18, 
      VA02_mfcc_sma.12. = 1.13196270572855e-17, VA02_mfcc_sma.13. = -4.09118309040634e-17, 
      VA02_mfcc_sma.14. = 8.56156119316215e-18, VA02_F0finEnv_sma = 1, 
      VA02_F0final_sma = 1, VA02_F0finEnv_sma = 11.2475000713838, VA02_F0final_sma = 13.6936948927086
      ), `389` = c(VA03_F0finEnv_sma = 1.58629187875084e-18, VA03_F0final_sma = -9.46461808448016e-17, 
      VA03_jitterLocal_sma = -2.36937840621222e-17, VA03_shimmerLocal_sma = 2.85381254827705e-17, 
      VA03_mfcc_sma.0. = -9.6294053113996e-17, VA03_mfcc_sma.1. = -5.49550802238737e-17, 
      VA03_mfcc_sma.2. = -2.20247732336464e-17, VA03_mfcc_sma.3. = -4.454892003863e-17, 
      VA03_mfcc_sma.4. = -7.9524696067784e-17, VA03_mfcc_sma.5. = -1.05680749785702e-17, 
      VA03_mfcc_sma.6. = -6.21185832013518e-17, VA03_mfcc_sma.7. = 4.15417752575213e-18, 
      VA03_mfcc_sma.8. = -8.15134676706638e-18, VA03_mfcc_sma.9. = 1.99531361473134e-18, 
      VA03_mfcc_sma.10. = 3.1218480555731e-17, VA03_mfcc_sma.11. = 2.1815428854396e-17, 
      VA03_mfcc_sma.12. = 8.15963331541171e-17, VA03_mfcc_sma.13. = 6.16448325097666e-19, 
      VA03_mfcc_sma.14. = 2.41651014444211e-17, VA03_F0finEnv_sma = 1, 
      VA03_F0final_sma = 1, VA03_F0finEnv_sma = 3.91835747651944, VA03_F0final_sma = 7.13223541696321
      ))
  </code></pre>