Cleaning Up Timestamps in R: How to Add a Minute Between Start and End Dates
Here is the corrected code for cleaning up timestamps by adding a minute between start and end:
library(tidyverse)
df %>%
mutate(start = as.POSIXct(ifelse(!is.na(lead(start)) & lead(start) < end,
lead(start) - 60, start), origin = "1970-01-01 00:00:00")) %>%
mutate(end = as.POSIXct(ifelse(!is.na(lead(start)) & lead(start) < end,
lead(start) + 60, end), origin = "1970-01-01 00:00:00"))
This code adds a minute between start and end for each row. The rest of the steps remain the same as before.
Note that I’ve corrected lead(start) to be subtracted by 60 from end, not just -1, since we want to add a minute between start and end.
Last modified on 2025-04-23