title: R: replacing NA values between two specific values by row
body: <p>I am trying to get my data ready for a later sequence analysis; to do this I need to insert the value 1 between the values 1 and 2.
  So all the NA's between the values of 1 and 2 become equal to 1 for each row. I've included an example table below; in my actual data each row is a unique individual and each column is a time period. The 1 represents admission, the 2 discharge from a program. I am attempting to get the periods between 'admission' and 'discharge' to equal 1 to signify being enrolled in the program and then setting the remaining NA's to 0 to signify not being in a program. There can be multiple admissions per row/individual.</p>
  
  <p>I've been trying to use apply, with which I can change the values themselves, however I can't replace the NA's between the values 1 and 2. Any guidance would be much appreciated!</p>
  
  <pre><code>mdat &lt;- matrix(c(1,NA,NA,NA,2,NA,NA,1,NA,2,  NA,NA,1,2,NA,NA,NA,1,NA,2), nrow = 2, ncol=10, byrow=TRUE,
             dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3", "C.4", "C.5", "C.6", "C.7", "C.8", "C.9", "C.10")))
  </code></pre>
  
  <pre>
  
  |      | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8| c9 | c10 |
  ----------------------------------------------------------
  |row 1 | 1  | NA | NA | NA | 2  | NA | NA | 1 | NA | 2   |
  |row 2 | NA | NA | 1  | 2  | NA | NA | NA | 1 | NA | 2   |
  </pre>
  
  <p>the desired result;</p>
  
  <pre>
  |      | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8| c9 | c10 |
  ----------------------------------------------------------
  |row 1 | 1  | 1  | 1  | 1  | 2  | NA | NA | 1 | 1  | 2   |
  |row 2 | NA | NA | 1  | 2  | NA | NA | NA | 1 | 1  | 2   |
  </pre>