title: Split dataset based on column with a loop
body: <p>I have been trying to get a loop that splits a dataset in multiple datasets based on a column value. However, the dataset is of a format I haven't handled before (i.e. a list containing both lists and data.tables). The dataset is reproducible by:</p>
  <pre><code>table1 &lt;- data.table::data.table(Scenario = 
                              c(rep(
                                c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), 
                                4)),
                            A = c(
                              rep(&quot;x&quot;, 4), rep(&quot;b&quot;, 4), rep(&quot;s&quot;, 4),
                              rep(&quot;u&quot;, 4)),
                            Correlation = c(1, 0.125, 0.1, 0, 
                                            0.125, 1, 0.2, 0, 
                                            0.1, 0.2,   1, 0, 
                                            0,     0,   0, 1),
                            Matrix = &quot;IM&quot;,
                            stringsAsFactors = FALSE,
                            check.names = FALSE)
  table2 &lt;- data.table::data.table(Scenario = 
                           c(rep(
                             c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), 
                             4)),
                         A = c(
                           rep(&quot;x&quot;, 4), rep(&quot;b&quot;, 4), rep(&quot;s&quot;, 4),
                           rep(&quot;u&quot;, 4)),
                         Correlation = c(1, 0.125, 0.1, 0, 
                                         0.125, 1, 0.2, 0, 
                                         0.1, 0.2,   1, 0, 
                                         0,     0,   0, 1),
                         Matrix = &quot;IM&quot;,
                         stringsAsFactors = FALSE,
                         check.names = FALSE)
  
  table3 &lt;- data.table::data.table(Scenario = 
                           c(rep(
                             c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), 
                             4)),
                         A = c(
                           rep(&quot;x&quot;, 4), rep(&quot;b&quot;, 4), rep(&quot;s&quot;, 4),
                           rep(&quot;u&quot;, 4)),
                         Correlation = c(1, 0.125, 0.1, 0, 
                                         0.125, 1, 0.2, 0, 
                                         0.1, 0.2,   1, 0, 
                                         0,     0,   0, 1),
                         Matrix = &quot;IM&quot;,
                         stringsAsFactors = FALSE,
                         check.names = FALSE)
  
  list1 &lt;- list(&quot;a&quot; = &quot;2019&quot;, &quot;b&quot; = &quot;2020&quot;, &quot;c&quot; = &quot;2021&quot;)
  list2 &lt;- list(&quot;a&quot; = &quot;test&quot;, &quot;b&quot; = &quot;test&quot;, &quot;c&quot; = &quot;test&quot;)
  
  input_data &lt;- list(&quot;table1&quot; = table1, &quot;table2&quot; = table2, &quot;table3&quot; = table3, 
  &quot;list1&quot;=list1, &quot;list2&quot; = list2)
  </code></pre>
  <p>I need a loop that splits this dataset based on all unique instances in the scenario column. The first dataset (for scenario value &quot;A&quot;) is reproducible by:</p>
  <pre><code>table1 &lt;- data.table::data.table(Scenario = 
                                 c(rep(
                                   c(&quot;A&quot;), 
                                   4)),
                               A = c(
                                 rep(&quot;x&quot;, 1), rep(&quot;b&quot;, 1), rep(&quot;s&quot;, 1),
                                 rep(&quot;u&quot;, 1)),
                               Correlation = c(1, 0.125, 0.1, 0 ),
                               Matrix = &quot;IM&quot;,
                               stringsAsFactors = FALSE,
                               check.names = FALSE)
  table2 &lt;- data.table::data.table(Scenario = 
                                 c(rep(
                                   c( &quot;A&quot;), 
                                   4)),
                               A = c(
                                 rep(&quot;x&quot;, 1), rep(&quot;b&quot;, 1), rep(&quot;s&quot;, 1),
                                 rep(&quot;u&quot;, 1)),
                               Correlation = c(1, 0.125, 0.1, 0),
                               Matrix = &quot;IM&quot;,
                               stringsAsFactors = FALSE,
                               check.names = FALSE)
  
  table3 &lt;- data.table::data.table(Scenario = 
                                 c(rep(
                                   c(&quot;A&quot;), 
                                   4)),
                               A = c(
                                 rep(&quot;x&quot;, 1), rep(&quot;b&quot;, 1), rep(&quot;s&quot;, 1),
                                 rep(&quot;u&quot;, 1)),
                               Correlation = c(1, 0.125, 0.1, 0),
                               Matrix = &quot;IM&quot;,
                               stringsAsFactors = FALSE,
                               check.names = FALSE)
  
  list1 &lt;- list(&quot;a&quot; = &quot;2019&quot;, &quot;b&quot; = &quot;2020&quot;, &quot;c&quot; = &quot;2021&quot;)
  list2 &lt;- list(&quot;a&quot; = &quot;test&quot;, &quot;b&quot; = &quot;test&quot;, &quot;c&quot; = &quot;test&quot;)
  
  input_data &lt;- list(&quot;table1&quot; = table1, &quot;table2&quot; = table2, &quot;table3&quot; = table3, 
                 &quot;list1&quot;=list1, &quot;list2&quot; = list2)
  </code></pre>
  <p>Please let me know if additional information is needed.</p>