title: "Transition" Variable in R
body: <p>Lets say I have these data set:</p>
  
  <pre><code>library(data.table)
  mydata &lt;- data.table(year=1991:2000,
                       z=c(0,0,1,1,1,1,1,0,0,0))
  </code></pre>
  
  <p>If I call the dataset, it will look something like this:</p>
  
  <pre><code>mydata
      year z
   1: 1991 0
   2: 1992 0
   3: 1993 1
   4: 1994 1
   5: 1995 1
   6: 1996 1
   7: 1997 1
   8: 1998 0
   9: 1999 0
  10: 2000 0
  </code></pre>
  
  <p>What I need is:</p>
  
  <ol>
  <li>A transition variable, call it <code>c</code>. If I had these dataset, it would look something like this:</li>
  </ol>
  
  <p><code>
          year z  c
       1: 1991 0  0
       2: 1992 0  0
       3: 1993 1  1
       4: 1994 1 NA
       5: 1995 1 NA
       6: 1996 1 NA
       7: 1997 1 NA
       8: 1998 0  0
       9: 1999 0  0
      10: 2000 0  0
  </code></p>
  
  <p>Essentially, <code>c</code> <em>marks</em> when there has been a <strong>transition</strong> in variable <code>z</code>, from  <code>z=0</code> to <code>z=1</code>. When it does, <code>c</code> puts a <code>1</code> just once and then starts putting <code>NA's</code> <em>until it returns to the original state</em> <code>(z=0)</code>. Then, it starts putting zeroes.</p>
  
  <ul>
  <li>I have another <code>id</code> variable, but that would complicate the example. I think I can manage that part. </li>
  </ul>
  
  <p>** EDITED **: In fact, it does not matter whether I have an <code>id</code> variable or not. </p>
  
  <p>It sounds easy, but not being a R expert myself, it's killing me!</p>