title: Finding within-groups specific sequences
body: <p>I am working on a data set including the results of repeated tests (results expressed as positive(1)/negative(0)) on individuals over time; the number of tests per individual is not necessarily the same.
  below is a df reproducing how my dataset looks like:</p>
  
  <pre><code>id&lt;-c(rep("a", time=5), rep("b", time=5), rep("c",time=7))
  date&lt;-as.Date(c("2018-03-01","2018-04-01","2018-06-01","2018-08-01","2018-10-01","2017-03-01","2017-04-01","2018-02-01","2018-11-01","2018-12-01","2016-05-11","2017-10-01","2018-03-01","2018-03-21","2018-4-01","2018-07-01","2018-08-01"))
  test&lt;-c(1,1,0,1,0,0,1,0,1,1,0,0,1,0,0,1,0)
  df&lt;-data.frame(id, test, date)
  df
  id test       date
  a    1 2018-03-01
  a    1 2018-04-01
  a    0 2018-06-01
  a    1 2018-08-01
  a    0 2018-10-01
  b    0 2017-03-01
  b    1 2017-04-01
  b    0 2018-02-01
  b    1 2018-11-01
  b    1 2018-12-01
  c    0 2016-05-11
  c    0 2017-10-01
  c    1 2018-03-01
  c    0 2018-03-21
  c    0 2018-04-01
  c    1 2018-07-01
  c    0 2018-08-01
  </code></pre>
  
  <p>what I am trying to do is to create a new column 'Var' indicating whether any of the following set of results: </p>
  
  <pre><code>match1&lt;-c(1,1,1,1)
  match2&lt;-c(1,1,1,0)
  match3&lt;-c(0,1,1,1)
  match4&lt;-c(1,0,1,1)
  match5&lt;-c(1,1,0,1)
  </code></pre>
  
  <p>is observed in the result set of each individual. ideally this would result in:</p>
  
  <pre><code>id test       date      Var
  a    1 2018-03-01     case
  a    1 2018-04-01     case
  a    0 2018-06-01     case
  a    1 2018-08-01     case
  a    0 2018-10-01     case
  b    0 2017-03-01     case
  b    1 2017-04-01     case
  b    0 2018-02-01     case
  b    1 2018-11-01     case
  b    1 2018-12-01     case
  c    0 2016-05-11 non-case
  c    0 2017-10-01 non-case
  c    1 2018-03-01 non-case
  c    0 2018-03-21 non-case
  c    0 2018-04-01 non-case
  c    1 2018-07-01 non-case
  c    0 2018-08-01 non-case
  </code></pre>
  
  <p>because the sequence (1,1,0,1) is observed within the result set of 'a', (1,0,1,1) in 'b' while none of the target sequences is observed in 'c'.</p>
  
  <p>Apologize for not posting any attempt but I am really stuck with this!
  best regards,
  Matteo</p>