Skip to content

generating a sequence of seconds

15 messages · Erin Hodgess, William Dunlap, John McKown +2 more

#
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
#
On Aug 12, 2014, at 1:51 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:

            
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")
[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"
[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"
[1] 0 1 2 3 4 5
[1] 86394 86395 86396 86397 86398 86399


Regards,

Marc Schwartz
#
Why do you want such a thing?  E.g., do you want it to print the time
of day without the date?  Or are you trying to avoid numeric problems
when you do regressions with the seconds-since-1970 numbers around
1414918800?  Or is there another problem you want solved?

Note that the number of seconds in a day depends on the day and the
time zone.  In US/Pacific time I get:

  > length(seq(from=as.POSIXct("2014-08-12
00:00:00"),to=as.POSIXct("2014-08-12 23:59:59"), by="secs"))
  [1] 86400
  > length(seq(from=as.POSIXct("2014-03-09
00:00:00"),to=as.POSIXct("2014-03-09 23:59:59"), by="secs"))
  [1] 82800
  > length(seq(from=as.POSIXct("2014-11-02
00:00:00"),to=as.POSIXct("2014-11-02 23:59:59"), by="secs"))
  [1] 90000

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Tue, Aug 12, 2014 at 11:51 AM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
#
What I would like to do is to look at several days and determine activities
that happened at times on those days.  I don't really care which days, I
just care about what time.

Thank you!
On Tue, Aug 12, 2014 at 3:14 PM, William Dunlap <wdunlap at tibco.com> wrote:

            

  
    
#
If your activities of interest are mainly during the workday then
seconds-since-3am might give good results, avoiding most daylight
savings time issues.  If they are more biologically oriented then
something like seconds before or after sunrise or sunset might be
better.  Both can be expressed as differences between POSIXct times.
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Tue, Aug 12, 2014 at 12:26 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
#
On Tue, Aug 12, 2014 at 1:51 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
I'm a bit confused by this request. The definition of a POSIXct is:
Class "POSIXct" represents the (signed) number of seconds since the
beginning of 1970 (in the UTC time zone) as a numeric vector.

So I don't really know what you mean by the "seconds portion". There
are 24*60*60 or 86,400 seconds in a day. Those seconds are from +0 at
00:00:00 to +86399 for 23:59:59. Is this what you were asking?

seconds_vector <-0:86399; #is the simple way to get the above.

By the definition given above, there is no such thing as a POSIXct
value without a date portion. Any number value will convert to a
date+time. Like a timestamp variable in SQL vs. a time variable.

If you want to display the seconds_vector as "HH:MM:SS" for some
reason, the simple way is:

character_time=sprintf("%02d:%02d:%02d", # C-style formatting string
                                 seconds_vector/3600, # hour value
                                 (seconds_vector%%3600)/60, #minute value
                                 seconds_vector%%60); #second value

You can simply make that a function

getTimePortion <- function(POSIXct_value) {
                            value_in_seconds=as.integer(POSIXct_value);
                            sprintf("%02d:%02d:%02d", # C-style
formatting string
                                     seconds_vector/3600, # hour value
                                     (seconds_vector%%3600)/60, #minute value
                                     seconds_vector%%60); #second value
                  };
#
Erin,

Is a sequential resolution of seconds required, as per your original post?

If so, then using my approach and specifying the start and end dates and times will work, with the coercion of the resultant vector to numeric as I included. The method I used (subtracting the first value) will also give you the starting second as 0, or you can alter the math to adjust the origin of the vector as you desire.

As Bill notes, there will be some days where the number of seconds in the day will be something other than 86,400. In Bill's example, it is due to his choosing the start and end dates of daylight savings time in a relevant time zone. Thus, his second date is short an hour, while the third has an extra hour.

Regards,

Marc
On Aug 12, 2014, at 2:26 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:

            
#
On Tue, Aug 12, 2014 at 2:40 PM, John McKown
<john.archie.mckown at gmail.com> wrote:
<snip>
Sorry, cut'n'pasted that incorrectly

getTimePortion <- function(POSIXct_value) {
                            value_in_seconds=as.integer(POSIXct_value);
                            sprintf("%02d:%02d:%02d", # C-style
                                     value_in_seconds/3600, # hour value
                                     (value_in_seconds%%3600)/60, #minute value
                                     value_in_seconds_vector%%60); #second value
                  };

And the above is "vectorized" and will work if argument has multiple
values in it.
#
And some people wonder why I absolutely abhor daylight saving time.
I'm not really fond of leap years and leap seconds either. Somebody
needs to fix the Earth's rotation and orbit!
On Tue, Aug 12, 2014 at 2:14 PM, William Dunlap <wdunlap at tibco.com> wrote:

  
    
#
On Aug 12, 2014, at 2:49 PM, John McKown <john.archie.mckown at gmail.com> wrote:

            
I have been a longtime proponent of slowing the rotation of the Earth on its axis, so that we could have longer days to be more productive.

Unfortunately, so far, my wish has gone unfulfilled...at least as it is relevant within human lifetimes.

;-)

Regards,

Marc
#
On Tue, Aug 12, 2014 at 2:26 PM, Erin Hodgess <erinm.hodgess at gmail.com>
wrote:
activities
Ah! A light dawns. You want to subset your data based on some part of the
time. Such as "between 13:23:00 and 15:10:01 of each day in the sample."
Ignoring the DST issue, which I shouldn't. It is left as an exercise for
the reader. But usually 13:23 is 13*3600+23*60, 48180, seconds after
midnight. 15:10:01 is 15*3600+10*60+1, 54601, seconds after midnight.
Suppose you have a data.frame() in a variable called myData. Further
suppose that the POSIXct variable in this data.frame is called "when". You
want to subset this into another data.frame() and call it subsetMyData.

subsetMyData<-myData[as.integer(myData$when)%%86400 >= 48180 &
as.integer(myData$when)%%86400 <= 54601,];

Yes, this is ugly. You might make it look nicer, and be easier to
understand, by:

startTime <- as.integer(as.difftime("13:23:00",units="secs")); # start on
or after 1:23 p.m.
endTime <- as.integer(as.difftime("15:10:01",units="secs")); # end on or
before 3:10:01 p.m.
testTime <- as.integer(myData$when)%%86400; #convert to seconds and
eliminate date portion.
subsetMyData <-myData[testTime >= startTime & testTime <= endTime,];

This will work best if myData$when is in GMT instead of local time. Why? No
DST worries. Again, in my opinion, all time date should be recorded in GMT.
Only convert to local time when displaying the data to an ignorant user who
can't handle GMT. Personally, I love to tell people something like: "it is
13:59:30 zulu". In my time zone, today, that is 08:59:30 a.m.
#
It depends on context.  If you are studying traffic flow or
electricity usage, then you want local time with all its warts
(perhaps stated as time since 3am so any daylight savings time
problems are confined to a small portion of the data), perhaps along
with time since sunrise and time since sunset.

If you are studying astronomy, then UTC is appropropriate.


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Aug 12, 2014 at 1:16 PM, John McKown
<john.archie.mckown at gmail.com> wrote:
#
Marc:

You just need to be more patient -- this is already happening:

http://en.wikipedia.org/wiki/Tidal_acceleration

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll
On Tue, Aug 12, 2014 at 1:10 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
#
On Tue, Aug 12, 2014 at 3:23 PM, William Dunlap <wdunlap at tibco.com> wrote:

            
I see your point. But if my data is in GMT, that is a unique timestamp
value. And, given that, along with location information, I should then be
able to generate a local time for "human activity". E.g. when do people go
to lunch? Another plus of this is that there is no confusion during "fall
back" whether this is the 1st or 2nd instance of something like 02:27:00.
Long ago, I worked for a city government. The recorded everything on the
machine in local time. Including police log entries. Always made me wonder
why some lawyer didn't have a nice window of confusion if something
allegedly happened on time change day and was logged as 02:30:00.
#
Great!

Thank you!

I think the function with the C-like function should do the trick.




On Tue, Aug 12, 2014 at 4:31 PM, John McKown <john.archie.mckown at gmail.com>
wrote: