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).
site <- c('A','B','C','D','E','F','G') #site
time <- c(0,1,4,0,3,2,0) # time in which even occured
event <- c(0,1,1,0,1,1,0) # did a event occur
data <- data.frame(site, time, event)
site.time.matrix <- 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
I have found some promising code using dplyr e.g. (Replacing more than one elements with replace function or Apply function to each column in a data frame observing each columns existing data type) which replaces values, although I am unsure of how to specify the adjacent cell in subsequent columns argument.
My apologies if this question is unclear, this is my first post on StackOverflow.
Thank you.