title: How to create variables that indicate start and end periods in a time series in R
body: <p>I want to calculate when someone starts and stops commenting on a blog. My dataset has a userid in column 1, followed by 6 columns, each representing the number of comments in a time period. I'd like to create a column ('active') identifying the first period where a non-zero value appears. I'd also like to create a column ('inactive') that identifies the first period where a zero follows a non-zero value and is followed only by zeros in subsequent periods.</p>
  
  <p>This is 10 rows of sample data:</p>
  
  <pre><code>structure(list(userid = c(199782L, 30982L, 27889L, 108358L, 29620L, 
  229214L, 37531L, 711L, 30516L, 32360L), Period1 = c(0L, 1L, 43L, 
  0L, 189L, 0L, 0L, 142L, 26L, 0L), Period2 = c(0L, 36L, 40L, 18L, 
  32L, 0L, 6L, 55L, 159L, 0L), Period3 = c(0L, 68L, 25L, 110L, 
  1L, 0L, 31L, 14L, 32L, 0L), Period4 = c(0L, 45L, 0L, 91L, 0L, 
  0L, 54L, 1L, 0L, 0L), Period5 = c(93L, 27L, 57L, 0L, 0L, 35L, 
  79L, 4L, 0L, 26L), Period6 = c(132L, 47L, 37L, 4L, 0L, 186L, 
  50L, 2L, 0L, 191L)), .Names = c("userid", "Period1", "Period2", 
  "Period3", "Period4", "Period5", "Period6"), row.names = 175:184, class = "data.frame")
  </code></pre>
  
  <p>Selected output for 5 of the rows follow. The absence of a value for 'inactive' means that the user is still active.:</p>
  
  <pre><code>userid, active, inactive
  199782, 5
  27889, 1
  29620, 1, 3
  37531, 2
  30516, 1, 3
  </code></pre>
  
  <p>Can someone point me in the right direction on how to approach this? Thanks!</p>