Select grouped rows with at least one matching criterion
<p>I want to select all those groupings that contain at least one of the elements that I am interested in. I was able to do this by creating an intermediate array, but I am looking for something simpler and faster. This is because my actual data set has over 1M rows (and 20 columns) so I am not sure whether I will have sufficient memory to create an intermediate array. More importantly, the below method on my original file takes a lot of time.</p>

<p>Here's my code and data:</p>

<p>a) Data</p>

<pre><code>dput(Data_File)
structure(list(Group_ID = c(123, 123, 123, 123, 234, 345, 444, 
444), Product_Name = c("ABCD", "EFGH", "XYZ1", "Z123", "ABCD", 
"EFGH", "ABCD", "ABCD"), Qty = c(2, 3, 4, 5, 6, 7, 8, 9)), .Names = c("Group_ID", 
"Product_Name", "Qty"), row.names = c(NA, 8L), class = "data.frame")
</code></pre>

<p>b) Code: I want to select <code>Group_ID</code> that has at least one <code>Product_Name = ABCD</code></p>

<pre><code>#Find out transactions
    Data_T &lt;- Data_File %&gt;% 
      group_by(Group_ID) %&gt;%
      dplyr::filter(Product_Name == "ABCD") %&gt;%
      select(Group_ID) %&gt;%
      distinct()

    #Now filter them
    Filtered_T &lt;- Data_File %&gt;% 
      group_by(Group_ID) %&gt;%
      dplyr::filter(Group_ID %in% Data_T$Group_ID)
</code></pre>

<p>c) Expected output is </p>

<pre><code>  Group_ID Product_Name   Qty
     &lt;dbl&gt;        &lt;chr&gt; &lt;dbl&gt;
      123         ABCD     2
      123         EFGH     3
      123         XYZ1     4
      123         Z123     5
      234         ABCD     6
      444         ABCD     8
      444         ABCD     9
</code></pre>

<p>I'm struggling with this for over 3 hours now. I looked at the auto-suggested thread by SO: <a href="https://stackoverflow.com/questions/35748051/select-rows-with-at-least-two-conditions-from-all-conditions">Select rows with at least two conditions from all conditions</a> but my question is very different.</p>