'How to insert rows in a data.frame

I have a data.frame which includes patient arrival times and their departure time for a clinic. At the moment, there are no gaps between patients. I would like to physically add in rows with a specific amount of time gap between each patient.

set.seed(200)

patient.time.in <- 
        c("09:00:00","09:03:00",
          "09:30:00","09:38:00",
          "10:00:00","10:30:00",
          "11:00:00","11:05:00",
          "12:00:00","12:30:00",
          "14:30:00","15:30:00")

patient.date.in <- "2022/03/29"



appointment.length <- c(runif(n=NROW(patient.time.in),min=10,max=90))

patient.infection <- sample(c("C","P","NA"),replace = TRUE,prob = c(1/2,1/3,1-1/2+1/3),size = NROW(patient.time.in))# c("P","P","NA","P","C","NA","C","P","NA","NA","C","P")

patient.roster <- data.frame(
                          ID=seq(1:12),
                          patient.time.in=lubridate::ymd_hms(paste(patient.date.in,patient.time.in)),
                          patient.time.out=lubridate::ymd_hms(paste(patient.date.in,patient.time.in))+lubridate::minutes(round(appointment.length)),
                          patient.infection=patient.infection,
                          seen.yet=rep("No"),
                          binary.seen.yet=0,
                          room=0)

Desired output

patient.roster
   ID     patient.time.in    patient.time.out patient.infection comment binary.seen.yet room    clash
1   1 2022-03-29 09:00:00 2022-03-29 09:57:00                 P      None               1    E No Clash
2   2 2022-03-29 09:57:00 2022-03-29 10:02:00                 NA      Gap               1    E No Clash
3   3 2022-03-29 09:03:00 2022-03-29 09:28:00                 P      None               1    G     Wait
4   4 2022-03-29 09:28:00 2022-03-29 09:33:00                 NA      Gap               1    G     Wait

Duplicating rows - This just doubles the rows

rbind(patient.roster, patient.roster[rep(1:12, 2), ])%>% arrange(ID)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source