[R] generating a sequence of seconds

Marc Schwartz marc_schwartz at me.com
Tue Aug 12 21:05:35 CEST 2014


On Aug 12, 2014, at 1:51 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:

> Hello!
> 
> If I would like to generate a sequence of seconds for a date, I would do
> the following:
> 
> x <- seq(from=as.POSIXct(2014-08-12 00:00:00),to=as.POSIXct(2014-08-12
> 23:59:59),by="secs")
> 
> What if I just want the seconds vector without the date, please?  Is there
> a convenient way to create such a vector, please?
> 
> thanks,
> Erin


Erin,

Do you want just the numeric vector of seconds, with the first value being 0, incrementing by 1 to the final value?

x <- seq(from = as.POSIXct("2014-08-12 00:00:00"), 
         to = as.POSIXct("2014-08-12 23:59:59"), 
         by = "secs")

> head(x)
[1] "2014-08-12 00:00:00 CDT" "2014-08-12 00:00:01 CDT"
[3] "2014-08-12 00:00:02 CDT" "2014-08-12 00:00:03 CDT"
[5] "2014-08-12 00:00:04 CDT" "2014-08-12 00:00:05 CDT"

> tail(x)
[1] "2014-08-12 23:59:54 CDT" "2014-08-12 23:59:55 CDT"
[3] "2014-08-12 23:59:56 CDT" "2014-08-12 23:59:57 CDT"
[5] "2014-08-12 23:59:58 CDT" "2014-08-12 23:59:59 CDT"


> head(as.numeric(x - x[1]))
[1] 0 1 2 3 4 5

> tail(as.numeric(x - x[1]))
[1] 86394 86395 86396 86397 86398 86399


Regards,

Marc Schwartz



More information about the R-help mailing list