R dplyr: Find a specific value in a column, then replace the adjacent cell in the subsequent columns to the right with that value
<p>I am trying to create a matrix of site and time-of-event. In my case, once the event has occurred ("1") it is permanent and cannot go back to a "0". Once a cell in a column is a "1" I am trying to populate the adjacent cell in the subsequent columns to the right with a "1" (see bellow example). </p>

<pre><code>site &lt;- c('A','B','C','D','E','F','G') #site
time &lt;- c(0,1,4,0,3,2,0) # time in which even occured
event &lt;- c(0,1,1,0,1,1,0) # did a event occur
data &lt;- data.frame(site, time, event)

site.time.matrix &lt;- cast(data, site~time)

# This is the output      # This is the desired output
#site   0  1  2  3  4     #site   0  1  2  3  4
#    A  0 NA NA NA NA     #    A  0  0  0  0  0
#    B NA  1 NA NA NA     #    B  0  1  1  1  1
#    C NA NA NA NA  1     #    C  0  0  0  0  1
#    D  0 NA NA NA NA     #    D  0  0  0  0  0
#    E NA NA NA  1 NA     #    E  0  0  0  1  1
#    F NA NA  1 NA NA     #    F  0  0  1  1  1
#    G  0 NA NA NA NA     #    G  0  0  0  0  0
</code></pre>

<p>I have found some promising code using dplyr e.g. (<a href="https://stackoverflow.com/questions/28250018/replacing-more-than-one-elements-with-replace-function">Replacing more than one elements with replace function</a> or <a href="https://stackoverflow.com/questions/7303322/apply-function-to-each-column-in-a-data-frame-observing-each-columns-existing-da">Apply function to each column in a data frame observing each columns existing data type</a>) which replaces values, although I am unsure of how to specify the adjacent cell in subsequent columns argument.</p>

<p>My apologies if this question is unclear, this is my first post on StackOverflow.</p>

<p>Thank you. </p>