title: Returning a list of data.tables from sapply
body: <p>I have an interesting problem.  I have some unknown number of ginormous tables, but at least 2.  They have the same schema and I want to pull subsets from each one and create a specialized cross join so I can combine various parts.</p>
  
  <p>So start with a simple example of 2 data.table objects:</p>
  
  <pre><code>DT1 &lt;- data.table(A = rep(1:2, each = 2), B = rep(1:2, 2), C = sample(4))
  DT2 &lt;- data.table(A = rep(1:2, 2), B = sample(4), C = rep(1:2, each = 2))
  
  &gt; DT1
  A B C
  1 1 2
  1 2 3
  2 1 1
  2 2 4
  
  &gt; DT2
  A B C
  1 2 1
  2 1 1
  1 3 2
  2 4 2
  </code></pre>
  
  <p>Because I ultimately don't know how many tables I will have to subset, I create a list and pass it to a function to do the subsetting using sapply:</p>
  
  <pre><code>tables = list(DT1, DT2)
  foo &lt;- function(dt, value) {
    result &lt;- dt[A == value,]
    return(result)
  }
  
  combined &lt;- sapply(tables, foo, value = 1)
  </code></pre>
  
  <p>The combined result, however, is a mismosh of vectors with no schema.  What I would like to get back is a list of data.tables.  The foo function returns a data.table but the result gets scrambled in the sapply.  How do I modify the code to perform an operation similar to this?</p>
  
  <pre><code>combined &lt;- list(DT1[A == 1,], DT2[A == 1])
  </code></pre>
  
  <p>Resulting in a list of data.tables</p>
  
  <pre><code>&gt; combined
  [[1]]
     A B C
  1: 1 1 2
  2: 1 2 3
  
  [[2]]
     A B C
  1: 1 2 1
  2: 1 3 2
  </code></pre>
  
  <p>TIA as always.</p>