[R] generating a sequence of seconds

John McKown john.archie.mckown at gmail.com
Tue Aug 12 22:16:13 CEST 2014

On Tue, Aug 12, 2014 at 2:26 PM, Erin Hodgess <erinm.hodgess at gmail.com>
> What I would like to do is to look at several days and determine
> that happened at times on those days.  I don't really care which days, I
> just care about what time.
> Thank you!

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.

There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

	[[alternative HTML version deleted]]

More information about the R-help mailing list