Clayton - 9 months ago 64

R Question

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`

`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`

Answer Source

You can use `mapply`

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