Re: [R] Weird POSIXct behaviour
I thought I had solved this problem. But I am still having trouble converting times. I am looking for a way to print out the string versions of times in different time zones. Same time, different zones. I have times stored as seconds since epoch and as text strings in local time. For instance (from an input file): Sat Nov 3 20:25:18 20121351927518 The local time zone is NZDT Browse[3] as.POSIXct(1351927518, origin=1970-01-01, tz=NZDT) [1] 2012-11-03 07:25:18 GMT Damn, that is GMT. But remembering the helpful replies to similar queries Browse[3] Sys.getenv(TZ) [1] GMT So I change the environment variable TZ and ... Browse[3] Sys.setenv(TZ=NZDT) Browse[3] as.POSIXct(1351927518, origin=1970-01-01, tz=NZDT) [1] 2012-11-03 07:25:18 NZDT The labled time zone is set OK but the time is wrong. The date string was generated in Perl as... DB7 p scalar(localtime(1351927518)) Sat Nov 3 20:25:18 2012 Using gmtime in Perl... DB8 p scalar(gmtime(1351927518)) Sat Nov 3 07:25:18 2012 cheers Worik On Fri, Mar 30, 2012 at 3:10 PM, Worik R wor...@gmail.com wrote: On Fri, Mar 30, 2012 at 2:53 PM, Joshua Ulrich josh.m.ulr...@gmail.comwrote: On Thu, Mar 29, 2012 at 3:56 PM, Worik R wor...@gmail.com wrote: I removed the (not so minimal) reproducible example because you can get the same behavior via: (s - Sys.time()) [1] 2012-03-29 20:43:35 CDT as.POSIXct(as.numeric(s),origin=1970-01-01) [1] 2012-03-30 02:43:35 CDT sapply() attempts to simplify to an array. Arrays can only contain an atomic type. POSIXct is not an atomic type, so it gets converted to numeric. The way to get around this is to explicitly set the timezone in your R session (see ?timezone). I can do this on my Ubuntu machine via: Sys.setenv(TZ=GMT) Now if I run the code above again, there is no difference after converting from POSIXct - numeric - POSIXct: (s - Sys.time()) [1] 2012-03-30 01:45:36 GMT as.POSIXct(as.numeric(s),origin=1970-01-01) [1] 2012-03-30 01:45:36 GMT HTH, Bingo! Thaks heaps. I have been working on this and had got as far as realising it was the conversion to numeric. I was trying to set the time zone in the as.POSIXct call but to no avail. But this looks good. cheers W -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Weird POSIXct behaviour
I have a reproducible example of my problem below On Mon, Mar 26, 2012 at 9:22 AM, Joshua Ulrich josh.m.ulr...@gmail.comwrote: Given two identical string representations of POSIXct objects, can the two objects represent different times? Yes. Here's an example (from my Ubuntu machine) of one way: (t1 - Sys.time()); (t2 - Sys.time())+0.001; t1 == t2 [1] 2012-03-25 15:13:48 CDT [1] 2012-03-25 15:13:48 CDT [1] FALSE options(digits.secs=3) (t1 - Sys.time()); (t2 - Sys.time())+0.001; t1 == t2 [1] 2012-03-25 15:17:36.520 CDT [1] 2012-03-25 15:17:36.523 CDT [1] FALSE That is interesting. Of course POSIXct includes more than seconds. The ones I have been using are rounded to seconds (I assume) so I forgot. SO I have made the effort and I think I have an example here that illustrates my problem. I am hoping there is an equally simple explanation, some little thing I have missed! ## First I build a little two row XTS in A() A - function() { M - matrix(ncol=2, byrow=TRUE, c( Tue Jan 10 00:00:02 2012, 0.7843, Tue Jan 10 00:00:40 2012, 0.7842)) L - data.frame(M, stringsAsFactors=FALSE) L[,2] - as.numeric(L[,2]) L.x - xts(L[,2], as.POSIXct(L[,1], format=%a %b %d %T %Y)) # L.x - make.index.unique(L.x, drop=TRUE) names(L.x) - c( Value) return(L.x) } ## This is a function that is called by sapply that returns the last index value from the XTS. f - function(i, DATA){ TT - end(DATA) cat(PAIR, as.character(TT), TT, \n) ret - TT return(ret) } ## Put it together... A.x - A() T - sapply(c(1), f, A.x) T - sapply(c(1), f, A.x) EUR.CHF 2012-01-10 00:00:40 1326106840 ## Note the date displayed as text and in seconds since epoch format. That was emitted by f() using cat() ## T is returned as numeric so needs to be converted to to a time to index A.x P - as.POSIXct(T, origin=1970-01-01) ## P should be the index to the last value of A.x cat(as.character(P), \n) cat(as.character(P), \n) 2012-01-09 12:00:40 ## It is 12 hours earlier A.x[P,] Value ## Not surprisingly there is no value returned T [1] 1326106840 ## Note that the returned time agrees that the returned time looks the same as that in A() ## Make a check Q - as.POSIXct(Tue Jan 10 00:00:40 2012, format=%a %b %d %T %Y) A.x[Q,] Value 2012-01-10 00:00:40 0.7842 I am hopelessly confused. Is there is some sort of transformation as the POSIXct is changed to numertic on return from sapply? cheers Worik [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Weird POSIXct behaviour
On Thu, Mar 29, 2012 at 3:56 PM, Worik R wor...@gmail.com wrote: I have a reproducible example of my problem below On Mon, Mar 26, 2012 at 9:22 AM, Joshua Ulrich josh.m.ulr...@gmail.com wrote: Given two identical string representations of POSIXct objects, can the two objects represent different times? Yes. Here's an example (from my Ubuntu machine) of one way: (t1 - Sys.time()); (t2 - Sys.time())+0.001; t1 == t2 [1] 2012-03-25 15:13:48 CDT [1] 2012-03-25 15:13:48 CDT [1] FALSE options(digits.secs=3) (t1 - Sys.time()); (t2 - Sys.time())+0.001; t1 == t2 [1] 2012-03-25 15:17:36.520 CDT [1] 2012-03-25 15:17:36.523 CDT [1] FALSE That is interesting. Of course POSIXct includes more than seconds. The ones I have been using are rounded to seconds (I assume) so I forgot. SO I have made the effort and I think I have an example here that illustrates my problem. I am hoping there is an equally simple explanation, some little thing I have missed! snip I am hopelessly confused. Is there is some sort of transformation as the POSIXct is changed to numertic on return from sapply? I removed the (not so minimal) reproducible example because you can get the same behavior via: (s - Sys.time()) [1] 2012-03-29 20:43:35 CDT as.POSIXct(as.numeric(s),origin=1970-01-01) [1] 2012-03-30 02:43:35 CDT sapply() attempts to simplify to an array. Arrays can only contain an atomic type. POSIXct is not an atomic type, so it gets converted to numeric. The way to get around this is to explicitly set the timezone in your R session (see ?timezone). I can do this on my Ubuntu machine via: Sys.setenv(TZ=GMT) Now if I run the code above again, there is no difference after converting from POSIXct - numeric - POSIXct: (s - Sys.time()) [1] 2012-03-30 01:45:36 GMT as.POSIXct(as.numeric(s),origin=1970-01-01) [1] 2012-03-30 01:45:36 GMT HTH, -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com __ R-help@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.
Re: [R] Weird POSIXct behaviour
On Fri, Mar 30, 2012 at 2:53 PM, Joshua Ulrich josh.m.ulr...@gmail.comwrote: On Thu, Mar 29, 2012 at 3:56 PM, Worik R wor...@gmail.com wrote: I removed the (not so minimal) reproducible example because you can get the same behavior via: (s - Sys.time()) [1] 2012-03-29 20:43:35 CDT as.POSIXct(as.numeric(s),origin=1970-01-01) [1] 2012-03-30 02:43:35 CDT sapply() attempts to simplify to an array. Arrays can only contain an atomic type. POSIXct is not an atomic type, so it gets converted to numeric. The way to get around this is to explicitly set the timezone in your R session (see ?timezone). I can do this on my Ubuntu machine via: Sys.setenv(TZ=GMT) Now if I run the code above again, there is no difference after converting from POSIXct - numeric - POSIXct: (s - Sys.time()) [1] 2012-03-30 01:45:36 GMT as.POSIXct(as.numeric(s),origin=1970-01-01) [1] 2012-03-30 01:45:36 GMT HTH, Bingo! Thaks heaps. I have been working on this and had got as far as realising it was the conversion to numeric. I was trying to set the time zone in the as.POSIXct call but to no avail. But this looks good. cheers W -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com [[alternative HTML version deleted]] __ R-help@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.
[R] Weird POSIXct behaviour
Friends I have an xts that I wish to access. Browse[2] DATA.ba[[p]][2012-03-20 00:59:57,bid] bid 2012-03-20 00:59:57 1.4993 So far so good. Now putting the index into a variable: Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] DATA.ba[[p]][Time, bid] bid Where has it gone? Looking closer Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) == Time [1] FALSE Browse[2] Browse[2] class(Time) [1] POSIXct POSIXt Browse[2] class(index(DATA.ba[[p]][2012-03-20 00:59:57,bid])) [1] POSIXct POSIXt So the variable 'Time' should be good to index DATA.ba[[p]]. I am hopelessly confused. cheers Worik [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Weird POSIXct behaviour
I see NZDT going in and out of your time values. Could that have something to do with it? On Mar 25, 2012, at 10:15 , Worik R wrote: Friends I have an xts that I wish to access. Browse[2] DATA.ba[[p]][2012-03-20 00:59:57,bid] bid 2012-03-20 00:59:57 1.4993 So far so good. Now putting the index into a variable: Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] DATA.ba[[p]][Time, bid] bid Where has it gone? Looking closer Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) == Time [1] FALSE Browse[2] Browse[2] class(Time) [1] POSIXct POSIXt Browse[2] class(index(DATA.ba[[p]][2012-03-20 00:59:57,bid])) [1] POSIXct POSIXt So the variable 'Time' should be good to index DATA.ba[[p]]. I am hopelessly confused. cheers Worik [[alternative HTML version deleted]] __ R-help@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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] Weird POSIXct behaviour
On Sun, Mar 25, 2012 at 3:15 AM, Worik R wor...@gmail.com wrote: Friends I have an xts that I wish to access. Browse[2] DATA.ba[[p]][2012-03-20 00:59:57,bid] bid 2012-03-20 00:59:57 1.4993 So far so good. Now putting the index into a variable: Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] DATA.ba[[p]][Time, bid] bid Where has it gone? It's hard to say, especially since you give no indication how you assigned the value to Time. A reproducible example, as requested in the posting guide, would be helpful. Also, what version of R, xts, and zoo are you using? Looking closer Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) == Time [1] FALSE Browse[2] Browse[2] class(Time) [1] POSIXct POSIXt Browse[2] class(index(DATA.ba[[p]][2012-03-20 00:59:57,bid])) [1] POSIXct POSIXt So the variable 'Time' should be good to index DATA.ba[[p]]. I am hopelessly confused. The printed representation of the index value and class cannot be used to accurately determine equality. cheers Worik -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com __ R-help@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.
Re: [R] Weird POSIXct behaviour
My bad. I should be clearer about the source of my confusion. Given two identical string representations of POSIXct objects, can the two objects represent different times? Where has it gone? It's hard to say, especially since you give no indication how you assigned the value to Time. A reproducible example, as requested in the posting guide, would be helpful. Also, what version of R, xts, and zoo are you using? Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) [1] 2012-03-20 00:59:57 NZDT A reproducible example would be huge at this point. WHat I need is an answer to that simple question. If the answer is No then it is worth doing the work for a reproducible example. If Yes I need to learn why and better ways of pasing the objects in and out of matrices and vectors. Using R version 2.12.1 The zoo documentation (?zoo) does not include a version. Where can I find it? Worik Looking closer Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) == Time [1] FALSE Browse[2] Browse[2] class(Time) [1] POSIXct POSIXt Browse[2] class(index(DATA.ba[[p]][2012-03-20 00:59:57,bid])) [1] POSIXct POSIXt So the variable 'Time' should be good to index DATA.ba[[p]]. I am hopelessly confused. The printed representation of the index value and class cannot be used to accurately determine equality. cheers Worik -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Weird POSIXct behaviour
On Sun, Mar 25, 2012 at 3:01 PM, Worik R wor...@gmail.com wrote: My bad. I should be clearer about the source of my confusion. Given two identical string representations of POSIXct objects, can the two objects represent different times? Yes. Here's an example (from my Ubuntu machine) of one way: (t1 - Sys.time()); (t2 - Sys.time())+0.001; t1 == t2 [1] 2012-03-25 15:13:48 CDT [1] 2012-03-25 15:13:48 CDT [1] FALSE options(digits.secs=3) (t1 - Sys.time()); (t2 - Sys.time())+0.001; t1 == t2 [1] 2012-03-25 15:17:36.520 CDT [1] 2012-03-25 15:17:36.523 CDT [1] FALSE Where has it gone? It's hard to say, especially since you give no indication how you assigned the value to Time. A reproducible example, as requested in the posting guide, would be helpful. Also, what version of R, xts, and zoo are you using? Browse[2] Time [1] 2012-03-20 00:59:57 NZDT Browse[2] index(DATA.ba[[p]][2012-03-20 00:59:57,bid]) [1] 2012-03-20 00:59:57 NZDT A reproducible example would be huge at this point. WHat I need is an answer to that simple question. If the answer is No then it is worth doing the work for a reproducible example. If Yes I need to learn why and better ways of pasing the objects in and out of matrices and vectors. No need for it to be huge. dput(DATA.ba[[p]][2012-03-20 00:59:57,bid]) would be a sufficient start. Using R version 2.12.1 The zoo documentation (?zoo) does not include a version. Where can I find it? From the output of sessionInfo(), or packageDescription(zoo). The output from sessionInfo() would be more helpful because it provides more information about your installation. Worik -- Joshua Ulrich | FOSS Trading: www.fosstrading.com R/Finance 2012: Applied Finance with R www.RinFinance.com __ R-help@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.