[R] Doing things with POSIXt

Dirk Eddelbuettel edd at debian.org
Tue Nov 27 03:31:55 CET 2001


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