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! ## 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.