I am wondering if there is a good way to work with data that is indexed in time, via timestamps with a resolution in milliseconds. As I understand it, the POSIX classes have a resolution i n terms of seconds, and will not process fractional seconds from a string. Is this correct. I realize that this may be a little unclear. Here is what I am trying to do: A data frame with a time series and a price series, there the time series is of the form: "2009-09-30 10:00:00.543" I ultimately like to create an xts object out of this data frame, or some other object where I can easily work with times (find out how much time has elapsed, between entries, etc). Using, for example, the code: as.POSIXct("2009-09-30 10:00:00.543", "%Y-%m-%d %H:%M:%S", tz="UTC") I find this returned: "2009-09-30 10:00:00 UTC">From various experiments similar to the above, and from the forums, it firstseemed like POSIX could not process millisecond time stamps. However, when I call: Sys.time() I get a POSIX object that has millisecond timestamps: "2011-03-24 13:11:52.79 EDT" This has made me confused. Does anyone know a way to go from a string containing time data to a POSIX object with millisecond timestamps? Thanks Madaliso [[alternative HTML version deleted]]
On 24 March 2011 at 13:13, Madaliso Mulaisho wrote: | I am wondering if there is a good way to work with data that is indexed in | time, via timestamps with a resolution in milliseconds. As I understand it, | the POSIX classes have a resolution i n terms of seconds, and will not | process fractional seconds from a string. Is this correct. I realize that | this may be a little unclear. Here is what I am trying to do: | | | | A data frame with a time series and a price series, there the time series is | of the form: | | "2009-09-30 10:00:00.543" | | | | I ultimately like to create an xts object out of this data frame, or some | other object where I can easily work with times (find out how much time has | elapsed, between entries, etc). | | | | Using, for example, the code: | | as.POSIXct("2009-09-30 10:00:00.543", "%Y-%m-%d %H:%M:%S", tz="UTC") | | I find this returned: | | "2009-09-30 10:00:00 UTC" | | | | >From various experiments similar to the above, and from the forums, it first | seemed like POSIX could not process millisecond time stamps. However, when | I call: | | Sys.time() | | I get a POSIX object that has millisecond timestamps: | | "2011-03-24 13:11:52.79 EDT" | | | | | | This has made me confused. Does anyone know a way to go from a string | containing time data to a POSIX object with millisecond timestamps? It all already works. You are simply being tricked by the common issue that _printed_ representation is not the same as _actual_ representation. See this: R> now <- Sys.time() R> now [1] "2011-03-24 15:21:25 CDT" R> options(digits.secs=6) ## switch to subsecond display R> now [1] "2011-03-24 15:21:25.347843 CDT" ## et voila, milliseconds revealed R> On Windows you get milliseconds, on operating systems with an X you get microseconds. All of this is stored as _fractional_ seconds since the epoch very neatly generalising the POSIX concept of integer seconds since the epoch. Dirk | | | Thanks | | Madaliso | | | [[alternative HTML version deleted]] | | ______________________________________________ | R-help at r-project.org mailing list | https://stat.ethz.ch/mailman/listinfo/r-help | PLEASE do read the posting guide http://www.R-project.org/posting-guide.html | and provide commented, minimal, self-contained, reproducible code. -- Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
?strptime or ?options> d1 <- as.POSIXct("2009-09-30 10:00:00.543") > d1[1] "2009-09-30 10:00:00 CDT"> # Oh, yeah, I forgot .... > op <- options(digits.secs=6) # or 3 > d1[1] "2009-09-30 10:00:00.543 CDT"> # The accuracy was there alreadyHTH, David L. Reiner, PhD Head Quant XR Trading LLC -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Madaliso Mulaisho Sent: Thursday, March 24, 2011 12:14 PM To: r-help at r-project.org Subject: [R] Millisecond TimeStamps I am wondering if there is a good way to work with data that is indexed in time, via timestamps with a resolution in milliseconds. As I understand it, the POSIX classes have a resolution i n terms of seconds, and will not process fractional seconds from a string. Is this correct. I realize that this may be a little unclear. Here is what I am trying to do: A data frame with a time series and a price series, there the time series is of the form: "2009-09-30 10:00:00.543" I ultimately like to create an xts object out of this data frame, or some other object where I can easily work with times (find out how much time has elapsed, between entries, etc). Using, for example, the code: as.POSIXct("2009-09-30 10:00:00.543", "%Y-%m-%d %H:%M:%S", tz="UTC") I find this returned: "2009-09-30 10:00:00 UTC">From various experiments similar to the above, and from the forums, it firstseemed like POSIX could not process millisecond time stamps. However, when I call: Sys.time() I get a POSIX object that has millisecond timestamps: "2011-03-24 13:11:52.79 EDT" This has made me confused. Does anyone know a way to go from a string containing time data to a POSIX object with millisecond timestamps? Thanks Madaliso [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws. Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates. THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND. TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.
On Mar 24, 2011, at 1:13 PM, Madaliso Mulaisho wrote:> I am wondering if there is a good way to work with data that is > indexed in > time, via timestamps with a resolution in milliseconds. As I > understand it, > the POSIX classes have a resolution i n terms of seconds, and will not > process fractional seconds from a string. Is this correct.No.> I realize that > this may be a little unclear. Here is what I am trying to do: > > A data frame with a time series and a price series, there the time > series is > of the form: > > "2009-09-30 10:00:00.543" > > I ultimately like to create an xts object out of this data frame, or > some > other object where I can easily work with times (find out how much > time has > elapsed, between entries, etc). > > Using, for example, the code: > > as.POSIXct("2009-09-30 10:00:00.543", "%Y-%m-%d %H:%M:%S", tz="UTC") > > I find this returned: > > "2009-09-30 10:00:00 UTC" > >> From various experiments similar to the above, and from the forums, >> it first > seemed like POSIX could not process millisecond time stamps. > However, when > I call: > > Sys.time() > > I get a POSIX object that has millisecond timestamps: > > "2011-03-24 13:11:52.79 EDT"By default the print method for POSIXt vectors does not display the subsecond values but they are not removed. > as.POSIXct("2009-09-30 10:00:00.543") [1] "2009-09-30 10:00:00 EDT" > format(as.POSIXct("2009-09-30 10:00:00.543"), format="%H:%M:%OS3") [1] "10:00:00.543"> > This has made me confused. Does anyone know a way to go from a string > containing time data to a POSIX object with millisecond timestamps?It is all explained in the help files. -- David Winsemius, MD West Hartford, CT
you can try this free online unix timestamp creator <http://www.online-code.net/unix-timestamp.html> , you can get the current time stamp in milliseconds. -- View this message in context: http://r.789695.n4.nabble.com/Millisecond-TimeStamps-tp3403594p4712598.html Sent from the R help mailing list archive at Nabble.com.