Clayton Clayton - 21 days ago 12
R Question

Create a list of time sequences from dates between 2 vectors

I'm new to loops. I would like to do the following, but for an entire vector:

library(timedate)
timeSequence("2014-05-09 10:48:00", "2014-05-11 19:39:00", by = "min")


Here is a sample:

Dates <- data.frame(Start = c("2014-05-09 10:48:00",
"2014-05-11 19:39:00",
"2014-10-09 08:52:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00"),
End = c("2014-05-11 19:39:00",
"2014-05-12 07:05:00",
"2014-10-29 10:48:59",
"2014-11-13 03:08:00",
"2014-11-13 08:41:59"))


I started with:

timeSequence(Dates$Start, Dates$End, by = "min)


But gets the error
'from' must be of length 1
...

So I've tried different iterations of
for
loops, with no success:

for (a in Dates$Start) {
for (b in Dates$End) {
Date_List <- timeSequence(a, b, by = "min")
}
}


Here I get
Error in seq.int(0, to0 - from, by) : wrong sign in 'by' argument
. I'd like to have a list where each item has the full sequence between dates in the vectors. Thank you so much.

Answer

You can use mapply

library(timeDate)

mapply(timeSequence, from = Dates$Start, to = Dates$End, by = "min", SIMPLIFY = FALSE)
Comments