title: Splitting and manipulating nested lists
body: <p>I'm trying to split a nested list by a group variable. Please consider the following structure:</p>
  
  <pre><code>&gt; str(L1)
  List of 2
   $ names:List of 2
    ..$ first: chr [1:5] "john" "lisa" "anna" "mike" ...
    ..$ last : chr [1:5] "johnsson" "larsson" "johnsson" "catell" ...
   $ stats:List of 2
    ..$ physical:List of 2
    .. ..$ age   : num [1:5] 14 22 53 23 31
    .. ..$ height: num [1:5] 165 176 179 182 191
    ..$ mental  :List of 1
    .. ..$ iq: num [1:5] 102 104 99 87 121
  </code></pre>
  
  <p>Now I need to produce two lists, which use both <code>L1$names$last</code> to splice, resulting in <code>L2</code> and <code>L3</code>, seen below: </p>
  
  <p><strong>L2: Result grouped by <code>L1$names$last</code></strong></p>
  
  <pre><code>&gt; str(L2) 
  List of 3
   $ johnsson:List of 2
    ..$ names:List of 1
    .. ..$ first: chr [1:2] "john" "anna"
    ..$ stats:List of 2
    .. ..$ physical:List of 2
    .. .. ..$ age   : num [1:2] 14 53
    .. .. ..$ height: num [1:2] 165 179
    .. ..$ mental  :List of 1
    .. .. ..$ iq: num [1:2] 102 99
   $ larsson :List of 2
    ..$ names:List of 1
    .. ..$ first: chr [1:2] "lisa" "steven"
    ..$ stats:List of 2
    .. ..$ physical:List of 2
    .. .. ..$ age   : num [1:2] 22 31
    .. .. ..$ height: num [1:2] 176 191
    .. ..$ mental  :List of 1
    .. .. ..$ iq: num [1:2] 104 121
   $ catell  :List of 2
    ..$ names:List of 1
    .. ..$ first: chr "mike"
    ..$ stats:List of 2
    .. ..$ physical:List of 2
    .. .. ..$ age   : num 23
    .. .. ..$ height: num 182
    .. ..$ mental  :List of 1
    .. .. ..$ iq: num 87
  </code></pre>
  
  <p><strong>L3: Each group only permits one occurrence of <code>L1$names$last</code></strong></p>
  
  <pre><code>List of 2
   $ 1:List of 2
    ..$ names:List of 2
    .. ..$ first: chr [1:3] "john" "lisa" "mike"
    .. ..$ last : chr [1:3] "johnsson" "larsson" "catell"
    ..$ stats:List of 2
    .. ..$ physical:List of 2
    .. .. ..$ age   : num [1:3] 14 22 23
    .. .. ..$ height: num [1:3] 165 176 182
    .. ..$ mental  :List of 1
    .. .. ..$ iq: num [1:3] 102 104 87
   $ 2:List of 2
    ..$ names:List of 2
    .. ..$ first: chr [1:2] "anna" "steven"
    .. ..$ last : chr [1:2] "johnsson" "larsson"
    ..$ stats:List of 2
    .. ..$ physical:List of 2
    .. .. ..$ age   : num [1:2] 53 31
    .. .. ..$ height: num [1:2] 179 191
    .. ..$ mental  :List of 1
    .. .. ..$ iq: num [1:2] 99 121
  </code></pre>
  
  <p>I`ve tried to apply <a href="https://stackoverflow.com/questions/43712843/split-list-by-id-and-store-results-into-seperate-vectors">this solution</a>, but it appears that this won't work for nested lists.</p>
  
  <p><strong>Reproducible code:</strong></p>
  
  <pre><code>L1 &lt;- list("names" = list("first" = c("john","lisa","anna","mike","steven"),"last" = c("johnsson","larsson","johnsson","catell","larsson")),"stats" = list("physical" = list("age" = c(14,22,53,23,31), "height" = c(165,176,179,182,191)), "mental" = list("iq" = c(102,104,99,87,121))))
  
  L2 &lt;- list("johnsson" = list("names" = list("first" = c("john","anna")),"stats" = list("physical" = list("age" = c(14,53), "height" = c(165,179)), "mental" = list("iq" = c(102,99)))), "larsson" = list("names" = list("first" = c("lisa","steven")),"stats" = list("physical" = list("age" = c(22,31), "height" = c(176,191)), "mental" = list("iq" = c(104,121)))), "catell" = list("names" = list("first" = "mike"),"stats" = list("physical" = list("age" = 23, "height" = 182), "mental" = list("iq" = 87))))
  
  L3 &lt;- list("1" = list("names" = list("first" = c("john","lisa","mike"),"last" = c("johnsson","larsson","catell")),"stats" = list("physical" = list("age" = c(14,22,23), "height" = c(165,176,182)), "mental" = list("iq" = c(102,104,87)))), "2" = list("names" = list("first" = c("anna","steven"),"last" = c("johnsson","larsson")),"stats" = list("physical" = list("age" = c(53,31), "height" = c(179,191)), "mental" = list("iq" = c(99,121)))))
  </code></pre>
  
  <p><strong>EDIT:</strong> Please note that the actual dataset is quite large and more deeply nested than the provided example.</p>