title: Subsetting Data Frame Based on Contents of a "Column" List
body: <h3>Set-Up</h3>
  
  <p>I have a list matrix, where one of the "columns" is a list (I realize it's an odd dataset to work with, but I find it useful for other operations). Each entry of the list is either; (1) empty (integer(0)), (2) an integer, or (3) a vector of integers. </p>
  
  <p>E.g. the R object "d.f", With d.f$ID an index vector, and d.f$Basket_List the list. </p>
  
  <pre><code>ID &lt;- c(1,2,3,4,5,6,7,8,9)
  Basket_List &lt;- list(integer(0),c(123,987),c(123,123),456,
                      c(456,123),456,c(123,987),c(987,123),987)
  d.f &lt;- data.frame(ID)
  d.f$Basket_List &lt;- Basket_List
  </code></pre>
  
  <h2>My Question</h2>
  
  <h3>Issue 1</h3>
  
  <p>I'd like to create a new dataset that's a subset of the initial, based on whether or not "Basket_List" contains certain value(s). E.g. a subset of all the rows in d.f such that Bask_list has "123" or "123" &amp; "987" -- or other more complicated conditions. </p>
  
  <p>I've tried every variation of the following, but to no avail. </p>
  
  <pre><code>d.f2 &lt;- subset(d.f, 123 %in% Basket_List)
  d.f2 &lt;- subset(d.f, 123 == any(Basket_List))
  d.f2 &lt;- d.f[which(123 %in% d.f$Basket_List,]
  # should return the subset, with rows 2,3,5,7 &amp; 8
  </code></pre>
  
  <h3>Issue 2</h3>
  
  <p>My other issue is that'd I'll be running this operation over many millions of rows (it's transaction data), so I'd like to optimize it as much as possible for speed (I have a complicated for loop now, but it takes too much time).  </p>
  
  <hr>
  
  <h3>Alternative Set-Up of Data</h3>
  
  <p>If you think it might be useful, the data might also be set-up as the following: </p>
  
  <pre><code>ID &lt;- c(1,2,2,3,3,4,5,5,6,7,7,8,8,9)
  Basket &lt;- c(NA,123,987,123,123,456,456,123,456,123,987,987,123,987)
  alt.d.f &lt;- data.frame(ID,Basket)
  </code></pre>