There is an issue in the initial loop through all Events, where we change "time" to be trial-based. We use the "time" variable to do the slicing:
Onset_index <- which.min(abs(Raw_data$time - Onset))
But then we change the same variable in the same loop, and that sometimes leads to errors.
I suggest creating a new variable before the loop.
A related issue is that many people start the tutorials without any preprocessing during data collection. however we modify system_time_stamp already at collection. What if we keep system_time_stamp until this point, and here we create the variable "time" which is trial-based?