I want to add one minute to the timestamp of specific cases. Say I have
timestamp <- seq(
from = as.POSIXct("12/05/2017 9:00", format = "%m/%d/%Y %H:%M"),
by = 60,
to = as.POSIXct("12/05/2017 9:02", format = "%m/%d/%Y %H:%M"))
ID <- seq(from = 101, by = 1, to = 115)
df <- data.frame(ID, timestamp)
To apply to a list of cases, I tried the following:
vars <- list(101, 102, 103)
foo <- function(x) {
as.POSIXct(df$encounter[dat$ID == x],
format = "%m/%d/%Y %H:%M") + 60
}
lapply(vars, foo)
which returns the correct responses in a list, but I am not able to coerce the results to a data frame so they can be added to the original file. I found this solution here for returning a data frame,
results <- lapply(vars, foo)
as.data.frame(do.call(rbind, results))
but it returns the timestamp as an unformatted numeric object. I tried indexing by case number
as.POSIXct(df$timestamp, format = "%m/%d/%Y %H:%M")[df$ID == 101] + 60
which prints the correct result to the screen, but I cannot figure out the correct syntax to replace the value(s) in the data frame itself.