[R] override date in xts time series

arun smartpink111 at yahoo.com
Sat Nov 3 02:35:48 CET 2012


HI Eric,

The example dataset seems to be working for me, but there is some problem with the order
y.1<-xts(1:6, as.POSIXct(y, format=fmt)) 
 y.1
#                    [,1]
#2004-04-04 01:00:00    4
#2004-04-04 01:15:00    1
#2004-04-04 01:30:00    2
#2004-04-04 01:30:00    5
#2004-04-04 01:45:00    3
#2004-04-04 03:30:00    6

index(y.1)<-update(index(y.1),month=unique(month(index(x.1))))
 y.1
#                    [,1]
#2004-01-04 01:00:00    4
#2004-01-04 01:15:00    1
#2004-01-04 01:30:00    2
#2004-01-04 01:30:00    5
#2004-01-04 01:45:00    3
#2004-01-04 03:30:00    6
I am using R 2.15.


If you look at the index of y.1, it is not unique.  I think you need unique index/timestamps to get it working correctly.
I think this is where it got into problems:
as.POSIXct(y,format=fmt)
#[1] "2004-04-04 01:15:00 EST" "2004-04-04 01:30:00 EST"
#[3] "2004-04-04 01:45:00 EST" "2004-04-04 01:00:00 EST"
#[5] "2004-04-04 01:30:00 EST" "2004-04-04 03:30:00 EDT"

Here, you can see that some of them are duplicated.

Now, see the difference:
as.POSIXct(y,format=fmt,tz="EST")
#[1] "2004-04-04 01:15:00 EST" "2004-04-04 01:30:00 EST"
#[3] "2004-04-04 01:45:00 EST" "2004-04-04 02:00:00 EST"
#[5] "2004-04-04 02:30:00 EST" "2004-04-04 03:30:00 EST"

Now, I am trying again:

y.1<-xts(1:6, as.POSIXct(y, format=fmt,tz="EST")) 
#You will get a warning message
#Warning message:
#timezone of object (EST) is different than current timezone (). 

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
#2004-01-04 02:00:00    4
#2004-01-04 02:30:00    5
#2004-01-04 03:30:00    6

I hope this helps.


A.K.







________________________________
From: Eric Morway <emorway at usgs.gov>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Friday, November 2, 2012 8:52 PM
Subject: Re: [R] override date in xts time series


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 <smartpink111 at yahoo.com>  
To:  Eric Morway <emorway at usgs.gov>  
Cc:  R help <r-help at 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 <emorway at usgs.gov>
To: r-help at 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 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.                                                          




More information about the R-help mailing list