I would like to create an end_time variable for each participant based on their start_times and time_end_of_experiment (minus say 10 ms), but quite unsure how to do this.
Here's a minimal working example
df <- data.frame("subject_nr" = c("1", "1", "1", "2", "2"),
"start_time" = c(50, 52, 55, 53, 54.5),
"time_end_of_experiment" = c(60, 60, 60, 55.5, 55.5))
subject_nr start_time time_end_of_experiment
1 1 50.0 60.0
2 1 52.0 60.0
3 1 55.0 60.0
4 2 53.0 55.5
5 2 54.5 55.5
Here's what the final product should look like
subject_nr start_time end_time time_end_of_experiment
1 1 50.0 51.9 60
2 1 52.0 54.9 60
3 1 55.0 59.9 60
4 2 53.0 54.4 55.5
5 2 54.5 55.4 55.5
CodePudding user response:
Here is how we could do it:
First use lead
to substract 0.1 from lead(start_time)
then for the last value in the groups use an ifelse statement to substract 0.1 from time_end_of_experiment:
library(dplyr)
df %>%
group_by(subject_nr) %>%
mutate(end_time = lead(start_time, default = last(start_time))-.1, .before=3) %>%
mutate(end_time = ifelse(start_time == last(start_time), time_end_of_experiment-.1, end_time))
subject_nr start_time end_time time_end_of_experiment
<chr> <dbl> <dbl> <dbl>
1 1 50 51.9 60
2 1 52 54.9 60
3 1 55 59.9 60
4 2 53 54.4 55.5
5 2 54.5 55.4 55.5