[R] Doing things with POSIXt

Vadim Ogranovich vograno at arbitrade.com
Tue Nov 27 00:54:14 CET 2001

Sorry, I sent an incomplete mail before, here is a complete one.

Dear R-Users,

I have a csv data file with timestamps and I wanted to use POSIXct time data
type to represent the respective column. I played around with the type and
found a couple of issues:

* there seems to be no direct way of reading datetimes into a variable.
Let's say this is my file, named t,
"1992-02-27 23:03:20 PST"
"1992-02-27 22:29:56 PST"
"1992-01-14 01:03:30 PST"

> scan("t", what=list(as.POSIXct("1992-01-14 01:03:30 PST")))
Error in scan("t", what = list(as.POSIXct("1992-01-14 01:03:30 PST"))) : 
	"scan" expected a real, got ""1992-02-27"

The only way to read datetimes I could come up with is to first read them as
characters and then convert using strptime. This is OK, but the way I tried
it looks cleaner to me.

* Suppose 'datetime' is a vector of POSIXct. I want to index all elements
that are after 1am. Apparently there is no easy way of doing this (for
example there is no access function daytime(POSIXt)), or hour(POSIXt), or
anything like this).
To write my own function I tried to follow this line:
datetime <- as.POSIXct("1992-01-14 01:03:30 PST")
midnight <- trunc.POSIXt(datetime, "days")
isAfter1am <- difftime(datetime, midnight, "hours") >= 1	# should
yield TRUE

This didn't work since difftime(datetime, midnight, "hours") returned 
'Time difference of 9.058333 hours'
which is wrong

If I call difftime without the "hours" argument, i.e. difftime(datetime,
midnight), then it works fine:
'Time difference of 1.058333 hours'
I could probably use this latter version, but I need to gain some confidence
and understand what's going on with difftime(datetime, midnight, "hours").

Any clue?

Thanks, Vadim

P.S. I run R on Red Hat 7.2
> R.Version()
[1] "i686-pc-linux-gnu"

[1] "i686"

[1] "linux-gnu"

[1] "i686, linux-gnu"

[1] ""

[1] "1"

[1] "3.1"

[1] "2001"

[1] "08"

[1] "31"

[1] "R"

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

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.


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