Hello Arun, My earlier example was not very robust. It seems issues arise during DST:
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)) y.1<-xts(1:6, as.POSIXct(y, format=fmt)) index(y.1)<-update(index(y.1),month=unique(month(index(x.1)))) y.1 # [,1] #2004-01-04 01:15:00 1 #2004-01-04 01:30:00 2 #2004-01-04 01:45:00 3 #<NA> 4 #<NA> 5 #2004-01-04 03:30:00 6 So, I'm wondering if there are built in functions/commands for dealing with this? The code here is a smaller example of a larger problem I'm working on. In the larger problem, the 'update' command you provided me with isn't updating, nor is it throwing an error. I'm wondering if its related to DST? Here is some example R output from the larger problem highlighting the behavior: xs[[i]][2:3] # xQ xSC #2004-01-01 00:15:00 0.43 NA #2004-01-01 00:30:00 0.43 2240 xs[[j]][2:3] # xQ xSC #2004-04-04 00:15:00 669 NA #2004-04-04 00:30:00 664 2320 The next line of code shows that the update command is return the desired result: update(index(xs[[j]][2:3]),month=unique(month(index(xs[[i]][2:3])))) # "2004-01-04 00:15:00 PST" "2004-01-04 00:30:00 PST" The next line shows what the current index is: index(xs[[j]][2:3]) # "2004-04-04 00:15:00 PST" "2004-04-04 00:30:00 PST" Now I try running the code you showed me index(xs[[j]][2:3])<-update(index(xs[[j]][2:3]),month=unique(month(index(xs[[i]][2:3])))) #Warning message: #In NextMethod(.Generic) : # number of items to replace is not a multiple of replacement length While there was a warning, there was no error, so I'm surprised that the index wasn't updated? xs[[j]][2:3] # xQ xSC #2004-04-04 00:15:00 669 NA #2004-04-04 00:30:00 664 2320 If its not day light savings, do you have any other thoughts as to what the problem might be? From: arun <smartpink...@yahoo.com> To: Eric Morway <emor...@usgs.gov> Cc: R help <r-help@r-project.org> Date: 11/02/2012 06:22 AM Subject: Re: [R] override date in xts time series HI, You can use: library(lubridate) index(y.1)<-update(index(y.1),month=1) #or index(y.1)<-update(index(y.1),month=unique(month(index(x.1)))) y.1 [,1] #2004-01-01 00:00:00 1 #2004-01-01 00:15:00 2 #2004-01-01 00:45:00 3 #2004-01-01 01:00:00 4 A.K. ----- Original Message ----- From: Eric Morway <emor...@usgs.gov> To: r-help@r-project.org Cc: Sent: Friday, November 2, 2012 7:55 AM Subject: [R] override date in xts time series Using the following bit of R, I'm wondering if there is a way to override/manipulate/replace the date in one xts time series with the date of another xts time series while not affecting/changing the times of the xts time series? library(xts) x.Date <- rep("1/1/2004",times=5) x.Times<- c("00:00:00", "00:15:00", "00:30:00", "00:45:00", "01:00:00") x<-paste(x.Date,x.Times) y.Date <- rep("3/1/2004",times=4) y.Times<- c("00:00:00", "00:15:00", "00:45:00", "01:00:00") y<-paste(y.Date,y.Times) fmt <- "%m/%d/%Y %H:%M:%S" x.1<-xts(1:5, as.POSIXct(x, format=fmt)) y.1<-xts(1:4, as.POSIXct(y, format=fmt)) If possible, I'd like query the date of x.1 and use it to override the date in y.1, leaving the times as they are. So the current output looks like this: x.1 # [,1] #2004-01-01 00:00:00 1 #2004-01-01 00:15:00 2 #2004-01-01 00:30:00 3 #2004-01-01 00:45:00 4 #2004-01-01 01:00:00 5 y.1 # [,1] #2004-03-01 00:00:00 1 #2004-03-01 00:15:00 2 #2004-03-01 00:45:00 3 #2004-03-01 01:00:00 4 But I would like change y.1 to look like the following by using the date of x.1 (notice the month was updated to match x.1): y.1 [,1] 2004-01-01 00:00:00 1 2004-01-01 00:15:00 2 2004-01-01 00:45:00 3 2004-01-01 01:00:00 4 Thanks, Eric ______________________________________________ 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. [[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.