Hello Arun, 

I too am using R 2.15 and am unable to get the same result as you.  You 
will notice in the R code that follows that when I use 'update' the time 
in the xts object goes haywire.  For example, "2004-04-04 01:15:00 EST" 
gets converted to "2004-01-03 22:15:00 PST" (see below).  Because I can't 
get the result you showed me in your previous response, and maybe you 
won't be able to get this result, I've resorted back to your other 
suggestion using gsub.  I don't have a good handle on regular expressions 
and was wondering if in the last line of code below, the replace month is 
'hardwired'?  In other words, could "\\101\\2" somehow be replaced with 
unique(month(index(x.1)))in the last line of code below so that x.1 is 
providing the replacement month, rather than have it fixed?  Or perhaps 
I've misunderstood the regular expression, which is entirely possible.


sessionInfo()
#R version 2.15.2 (2012-10-26)
#Platform: x86_64-w64-mingw32/x64 (64-bit)

library(xts)
library(lubridate)

x.Date <- rep("1/1/2004",times=5)
x.Times<- c("01:15:00", "01:30:00", "01:45:00",
               "02:00:00", "02:30:00", "03:00:00", "03:15:00")
x<-paste(x.Date,x.Times)

y.Date <- rep("4/4/2004",times=4)
y.Times<- c("01:15:00", "01:30:00", "01:45:00",
               "02:00:00", "02:30:00", "03:30:00")

y<-paste(y.Date,y.Times)

fmt <- "%m/%d/%Y %H:%M:%S"
x.1<-xts(1:7, as.POSIXct(x, format=fmt, tz = "EST"))
y.1<-xts(1:6, as.POSIXct(y, format=fmt, tz = "EST"))

y.1

#                    [,1]
#2004-04-04 01:15:00    1
#2004-04-04 01:30:00    2
#2004-04-04 01:45:00    3
#2004-04-04 02:00:00    4
#2004-04-04 02:30:00    5
#2004-04-04 03:30:00    6
#Warning message:
#timezone of object (EST) is different than current timezone (). 

index(y.1)
# "2004-04-04 01:15:00 EST" "2004-04-04 01:30:00 EST"
# "2004-04-04 01:45:00 EST" "2004-04-04 02:00:00 EST"
# "2004-04-04 02:30:00 EST" "2004-04-04 03:30:00 EST"

index(y.1)<-update(index(y.1),month=unique(month(index(x.1)))) 
index(y.1)

# "2004-01-03 22:15:00 PST" "2004-01-03 22:30:00 PST"
# "2004-01-03 22:45:00 PST" "2004-01-03 23:00:00 PST"
# "2004-01-03 23:30:00 PST" "2004-01-04 00:30:00 PST"


index(y.1)<-as.POSIXct(gsub("(.*\\-).*(\\-.*)","\\101\\2",index(y.1))) 
        [[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.

Reply via email to