[R] Doing things with POSIXt

Vadim Ogranovich vograno at arbitrade.com
Tue Nov 27 03:39:21 CET 2001


The catch is that the daytime of "1992-01-14 01:03:30 PST" is 1.058333 and
not 7.058333 as your system produces. On my system it is 9.058333 hours,
which makes me think it has something to do with time zones.

-----Original Message-----
From: Dirk Eddelbuettel [mailto:edd at debian.org]
Sent: Monday, November 26, 2001 6:32 PM
To: Vadim Ogranovich
Cc: 'r-help at stat.math.ethz.ch'
Subject: [R] Doing things with POSIXt



  "Vadim" == Vadim Ogranovich <vograno at arbitrade.com> writes:
  Vadim> * Suppose 'datetime' is a vector of POSIXct. I want to index all
  Vadim> elements that are after 1am. Apparently there is no easy way of
  Vadim> doing this (for example there is no access function
  Vadim> daytime(POSIXt)), or hour(POSIXt), or anything like this).  To
write
  Vadim> my own function I tried to follow this line: datetime <-
  Vadim> as.POSIXct("1992-01-14 01:03:30 PST") midnight <-
  Vadim> trunc.POSIXt(datetime, "days") isAfter1am <- difftime(datetime,
  Vadim> midnight, "hours") >= 1 # should yield TRUE
  Vadim> 
  Vadim> This didn't work since difftime(datetime, midnight, "hours")
  Vadim> returned 'Time difference of 9.058333 hours' which is wrong

You need to convert it to numeric

> datetime <- as.POSIXct("1992-01-14 01:03:30 PST")
> midnight <- trunc.POSIXt(datetime, "days")
> difftime(datetime, midnight, "hours")
Time difference of 7.058333 hours
> as.numeric(difftime(datetime, midnight, "hours"))
[1] 7.058333
> as.numeric(difftime(datetime, midnight, "hours")) >= 1
[1] TRUE

Dirk

-- 
Better to have an approximate answer to the right question 
than a precise answer to the wrong question. -- John Tukey

-------------------------------------------------- 
DISCLAIMER 
This e-mail, and any attachments thereto, is intended only for use by the
addressee(s) named herein and may contain legally privileged and/or
confidential information.  If you are not the intended recipient of this
e-mail, you are hereby notified that any dissemination, distribution or
copying of this e-mail, and any attachments thereto, is strictly prohibited.
If you have received this e-mail in error, please immediately notify me and
permanently delete the original and any copy of any e-mail and any printout
thereof. 

E-mail transmission cannot be guaranteed to be secure or error-free.  The
sender therefore does not accept liability for any errors or omissions in
the contents of this message which arise as a result of e-mail transmission.

NOTICE REGARDING PRIVACY AND CONFIDENTIALITY 

Knight Trading Group may, at its discretion, monitor and review the content
of all e-mail communications. 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list