title: Subsetting a range of values from a column variable based on the values of another value
body: <p>I am trying to keep all my rows in the dataframe but DROP the form 2 rows that do not fall with a minimum of 2 years of form 8.  </p>
  
  <pre><code>library(tidyverse)   
  
  forms &lt;- data_frame( CASEID = rep(01012,5), VISIT = c(450, 450, 365, 365, 450), FORM = c(18, 8, 7, 2, 2), DTYvisit = c(2006, 2006, 2003, 2003, 2006) )
  
  &gt; forms # A tibble: 5 x 4
  
  CASEID VISIT FORM YEAR   
  
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;   
  
  1 1012 450 18 2006
  
  2 1012 450 8 2006
  
  3 1012 365 7 2003
  
  4 1012 365 2 2003
  
  5 1012 450 2 2004
  
  6 1013 450 8 2003
  
  7 1013 450 18 2003
  
  8 1013 450 2 2003
  
  9 1012 450 2 2009
  </code></pre>
  
  <p>Any suggestions on how I could drop rows of FORM 2 that do not fall within a &lt; 2 year range of the FORM 8 DTyvisit?</p>
  
  <p>This worked great:</p>
  
  <pre><code>form2.matchedOnForm8 &lt;- forms %&gt;% group_by(CASEID) %&gt;% filter(FORM == 8) %&gt;% select(CASEID, VISIT, DTYvisit) %&gt;% left_join(filter(forms, FORM == 2), by = c("CASEID", "VISIT", "DTYvisit")) %&gt;% bind_rows(filter(forms, FORM != 2))
  </code></pre>
  
  <p>but now I am losing observations.</p>
  
  <p>I need the following:</p>
  
  <pre><code>library(tidyverse)
  
  forms &lt;- data_frame( CASEID = rep(01012,5), VISIT = c(450, 450, 365, 365, 450), FORM = c(18, 8, 7, 2, 2), DTYvisit = c(2006, 2006, 2003, 2003, 2006) )
  
  &gt; forms # A tibble: 5 x 4
  
  CASEID VISIT FORM YEAR   
  
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  
  1 1012 450 18 2006
  
  2 1012 450 8 2006
  
  3 1012 365 7 2003
  
  4 1012 450 2 2004
  
  5 1013 450 8 2003
  
  6 1013 450 18 2003
  
  7 1013 450 2 2003
  </code></pre>