Dear R-Users, I have a 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 "1992-02-27 23:03:20 PST" "1992-02-27 22:29:56 PST" "1992-01-14 01:03:30 PST" -------------------------------------------------- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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" then> 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()$platform [1] "i686-pc-linux-gnu" $arch [1] "i686" $os [1] "linux-gnu" $system [1] "i686, linux-gnu" $status [1] "" $major [1] "1" $minor [1] "3.1" $year [1] "2001" $month [1] "08" $day [1] "31" $language [1] "R" -------------------------------------------------- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Vadim" == Vadim Ogranovich <vograno at arbitrade.com> writes: Vadim> The catch is that the daytime of "1992-01-14 01:03:30 PST" is Vadim> 1.058333 and not 7.058333 as your system produces. On my system it Vadim> is 9.058333 hours, which makes me think it has something to do with Vadim> time zones.> datetime <- as.POSIXct("1992-01-14 01:03:30 PST", tz="PST") > as.numeric(difftime(datetime, midnight, "hours"))[1] 1.058333 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._