how to fill in values in a vector?
<p>I have vectors in R containing a lot of 0's, and a few non-zero numbers.Each vector starts with a non-zero number. </p>

<p>For example &lt;1,0,0,0,0,0,2,0,0,0,0,0,4,0,0,0> </p>

<p>I would like to set all of the zeros equal to the most recent non-zero number.</p>

<p>I.e. this vector would become &lt;1,1,1,1,1,1,2,2,2,2,2,2,4,4,4,4></p>

<p>I need to do this for a about 100 vectors containing around 6 million entries each. Currently I am using a for loop:</p>

<pre><code>for(k in 1:length(vector){

  if(vector[k] == 0){

    vector[k] &lt;- vector[k-1]
  }
}
</code></pre>

<p>Is there a more efficient way to do this?</p>

<p>Thanks!</p>