title: selecting groups by its scenario in R
body: <p>here the data</p>
  
  <pre><code>mydat=structure(list(code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L), .Label = "52382МСК", class = "factor"), 
      item = c(11709L, 11709L, 11709L, 11709L, 11708L, 11708L, 
      11708L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 
      11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 
      11710L, 11710L, 11710L, 11710L, 11710L, 11710L), sales = c(30L, 
      10L, 20L, 15L, 2L, 10L, 3L, 30L, 10L, 20L, 15L, 2L, 10L, 
      3L, 30L, 10L, 20L, 15L, 2L, 10L, 3L, 30L, 10L, 20L, 15L, 
      2L, 10L, 3L), action = c(0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 
      0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
      0L, 1L, 0L, 0L, 0L)), .Names = c("code", "item", "sales", 
  "action"), class = "data.frame", row.names = c(NA, -28L))
  </code></pre>
  
  <p>I have 3 groups vars code+item. Here 3 groups:</p>
  
  <pre><code>code    item
  52382МСК    11709
  52382МСК    11708
  52382МСК    11710
  </code></pre>
  
  <p>Also i have action column. It can have only two values zero(0) or one(1).</p>
  
  <p>each group represents 3 scenarios</p>
  
  <pre><code>52382МСК    11709
  </code></pre>
  
  <p>it is scenario when we have 1 zero category of action col. before first category of action col , and two zeros after first category of action col.
  Note: maybe scenario when  we have 2 zero category of action col. before first category of action col , and 1 zero after first category of action col.</p>
  
  <pre><code>52382МСК    11708
  </code></pre>
  
  <p>it is scenario when we have 1 zero category of action col. and 1 zeros after first category of action col.</p>
  
  <pre><code>52382МСК    11710
  </code></pre>
  
  <p>it is scenario when we have 3(or more) zero category of action col. and 3(or more) zeros after first category of action col.</p>
  
  <p>How can i select groups which have each of scenario?
  I.e <code>Mydat1</code> it is groups with first scenario,
  <code>Mydat2</code> it is groups with second scenario,
   and <code>Mydat3</code> it is groups with third scenario</p>
  
  <p>Output is simple</p>
  
  <pre><code>mydat1
  
  code    item    sales   action
  52382МСК    11709   30  0
  52382МСК    11709   10  1
  52382МСК    11709   20  0
  52382МСК    11709   15  0
  
  
  
  mydat2
  code    item    sales   action
  52382МСК    11708   2   0
  52382МСК    11708   10  1
  52382МСК    11708   3   0
  
  mydat3
  code    item    sales   action
  52382МСК    11710   30  0
  52382МСК    11710   10  0
  52382МСК    11710   20  0
  52382МСК    11710   15  1
  52382МСК    11710   2   0
  52382МСК    11710   10  0
  52382МСК    11710   3   0
  52382МСК    11710   30  0
  52382МСК    11710   10  0
  52382МСК    11710   20  0
  52382МСК    11710   15  1
  52382МСК    11710   2   0
  52382МСК    11710   10  0
  52382МСК    11710   3   0
  52382МСК    11710   30  0
  52382МСК    11710   10  0
  52382МСК    11710   20  0
  52382МСК    11710   15  1
  52382МСК    11710   2   0
  52382МСК    11710   10  0
  52382МСК    11710   3   0
  </code></pre>
  
  <h3>Edit</h3>
  
  <p>I forgot, it can be scenario  when we have 1 zero category of action col. before first category of action col , and three zeros after first category of action col.
  or maybe scenario when  we have 3 zero category of action col. before first category of action col , and 1 zero after first category of action col.</p>
  
  <pre><code>(mydat4)
  </code></pre>
  
  <p>Also can be 
  scenario  when we have 2 zero category of action col. before first category of action col , and three zeros after first category of action col.
  or maybe scenario when  we have 3 zero category of action col. before first category of action col , and 2 zero after first category of action col.</p>
  
  <pre><code>(mydat5)
  </code></pre>
  
  <p>I.E. it must work, only with these scenarios.</p>
  
  <h1>edit2</h1>
  
  <p>i found next group, it has only one row</p>
  
  <pre><code>code    item    sales   action
  52499МСК    11202   2   0
  </code></pre>
  
  <p>how to do that if data has only one row it would be 6 scenario?</p>
  
  <p>also can be so</p>
  
  <pre><code>code    item    sales   action
  52499МСК    11202   2   0
   52499МСК   11202   2   1
  </code></pre>
  
  <p>or </p>
  
  <pre><code>code    item    sales   action
  52499МСК    11202   2   0
   52499МСК   11202   2   0
  </code></pre>
  
  <p>or</p>
  
  <pre><code>code    item    sales   action
  52499МСК    11202   2   1
   52499МСК   11202   2   1
  </code></pre>
  
  <p>if we have only two rows in group, then 7 scenario</p>