Skip to content

Posix Problem, difftime

4 messages · Jason Turner, Matthew Pocernich, Don MacQueen +1 more

#
On Thu, Oct 17, 2002 at 11:51:24AM -0500, Matthew Pocernich wrote:
... [trouble with POSIX date/time classes] ...
Judging by the different column types this looks more like a data frame than
a matrix - minor picky point, but computers are even pickier ;-).
Correct.  aa$posit is a POSIXlt structure, which is effectively a 9 element list. 
Each list element is a vector as long as the number of rows in your data.frame.

Using the aa you've provided (data frame with 5 rows):
[1] 9
[1] 5
$sec
[1] 5

$min
[1] 5

$hour
[1] 5

$mday
[1] 5

$mon
[1] 5

$year
[1] 5

$wday
[1] 5

$yday
[1] 5

$isdst
[1] 5
Yes.  cbind works nicly with vectors of atomic data types.  It gets a little hairy with
lists.
As Larry Wall once said in answer to a perl question: 

Q. Why is this so clumsy?
A. The trick is to use [the language's] strengths rather than its weaknesses.
             -- Larry Wall in <8225 at jpl-devvax.JPL.NASA.GOV>

What's below works using R's wonderful indexing, with difftime.  Note that the first
element of the list is more properly NA than 0; there is no "before-the-first" 
observation, so time difference makes no sense for the first.
tide               posit diffh y365
1 -1.25 1901-10-28 22:00:00  -2.7  301
2 -2.75 1901-10-28 23:00:00  -1.5  301
3 -2.25 1901-10-29 00:00:00   0.5  302
4 -0.25 1901-10-29 01:00:00   2.0  302
5  2.65 1901-10-29 02:00:00   2.9  302
[1] 5
tide               posit diffh y365 difftime
1 -1.25 1901-10-28 22:00:00  -2.7  301       NA
2 -2.75 1901-10-28 23:00:00  -1.5  301        1
3 -2.25 1901-10-29 00:00:00   0.5  302        1
4 -0.25 1901-10-29 01:00:00   2.0  302        1
5  2.65 1901-10-29 02:00:00   2.9  302        1

# a quick check on which rows had difftime not equal to 1:
[1] tide     posit    diffh    y365     difftime
<0 rows> (or 0-length row.names)


[side issue, to answer the question that's probably in your head now:
"why are dates so hard in R?"]

Dates and times are implemented very thorougly and very solidly
in R.  

Dates and times themselves have a ludicrous, irrational, irregular 
structure, and are probably the hardest sticking point in any piece 
of programming.  Anything that handles dates solidly and in generality 
is going to be hard.  Even worse, differnt operating systems and 
programming environments all had their own, uh, "clever" solutions.  
Integrating this, and making it work the same way no matter where R 
is running is a very, very difficult thing.

Believe it or not, R makes dates and times much, much easier.
If it seems to have some bizarre corners, it's because dates and
times have some very very strange twists.

So, let me just thank Brian D. Ripley and Kurt Hornik once again 
for the wonderful job they've done on the date/time classes.
Good work, guys.

Cheers

Jason
#
I am having a series of problems using date time data that has been converted into a POSIXt and POSIXlt classes.  I have  hourly time series data from 1900 that has been converted from text data.
 
I assume most of my problems come from a mis-underdanding of the POSIX class.  My matrix named (aa) for this year is approx 8700 by 4.   When I try to calculate the length of posit column ( which is the date and time) I get
[1] 9
 
When I try to combined POSIX columns or manipulate them like matrices, I have problems such as
Error in cbind(...) : cannot create a matrix from these types
 
Ultimately I would like to check to make certain the hourly data is not missing.  I tried doing this using vectors such as difftime(aa$posit, lag(aa$posit)).  This didn't work.  Ultimately I used a for loop, which is very slow and it produces the following error.  The last column is the difference in times between subsequent posit values.  The fourth row shows a 2 hour gap between events.  (This did not happen in any of the other days.  ) 
 
 
     tide               posit diffh y365 diftime
7223 -1.25 1901-10-28 22:00:00  -2.7  301       1
7224 -2.75 1901-10-28 23:00:00  -1.5  301       1
7225 -2.25 1901-10-29 00:00:00   0.5  302       1
7226 -0.25 1901-10-29 01:00:00   2.0  302       2
7227  2.65 1901-10-29 02:00:00   2.9  302       1
 
Any help or suggestions are greatly appreciated.
 
Thanks,
 
Matt 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Your first step would be to use POSIXct, not POSIXlt.
POSIXlt objects are actually lists, and cbind() doesn't have meaning for lists.
POSIXct objects are actually numeric vectors, so they can be combined 
into matrices.

However, matrices are either numeric or character, so POSIXct objects 
combined into a matrix will get displayed in their numeric form:
[,1]      [,2]
[1,] 1010091600 978555600
[2,] 1012770000 981234000

Checking for missing hours is pretty easy with POSIXct objects:
[1] "1901-01-02 11:00:00 PST" "1901-01-02 12:00:00 PST" "1901-01-02 
13:00:00 PST" "1901-01-02 14:00:00 PST" "1901-01-02 16:00:00 PST"
[6] "1901-01-02 17:00:00 PST" "1901-01-02 18:00:00 PST"
[1] 3600 3600 3600 7200 3600 3600
[1] TRUE

Because an hour = 3600 seconds.

Regarding the 2 hour interval, have you considered that there may be 
a transition from daylight savings time to standard time? I don't 
know if that was in effect in 1901 in whatever timezone you're in, 
and if it was, whether 29 Oct was the day of transition, but it's 
worth considering. Other than that, in order to help, I'd have to see 
what your data looked like before it was in R, and what code you used 
to convert it to POSIXt.

-Don
At 11:51 AM -0500 10/17/02, Matthew Pocernich wrote:
Because it's a list with 9 elements. Try unclass(aa$posit) to see.

  
    
#
On Thu, 17 Oct 2002, Matthew Pocernich wrote:

            
As it says, you can't create matrix from these types.  Matrices can only
hold numbers or characters. You can create a dataframe.
No, lag() just adjusts the time series attributes of a vector.

You could do
  n<-length(a$posit)
  difftime(a$posit[1:(n-1)],a$posit[2:n])
At least in my time zone this is correct.  There was a 2 hour interval
between 1901-10-29 00:00:00 and 1901-10-29 01:00:00.  Note that 1901-10-28
is the last Saturday in October: it was the end of summer time.
[1] 0
[1] 1


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._