[R] override date in xts time series

arun smartpink111 at yahoo.com
Sat Nov 3 17:10:14 CET 2012


HI,

Could you check whether you are getting the same result with tz="GMT"?
as.POSIXct(x,format=fmt,tz="GMT")
#[1] "2004-01-01 01:15:00 GMT" "2004-01-01 01:30:00 GMT"
#[3] "2004-01-01 01:45:00 GMT" "2004-01-01 02:00:00 GMT"
#[5] "2004-01-01 02:30:00 GMT" "2004-01-01 03:00:00 GMT"
#[7] "2004-01-01 03:15:00 GMT"
 as.POSIXct(y,format=fmt,tz="GMT")
#[1] "2004-04-04 01:15:00 GMT" "2004-04-04 01:30:00 GMT"
#[3] "2004-04-04 01:45:00 GMT" "2004-04-04 02:00:00 GMT"
#[5] "2004-04-04 02:30:00 GMT" "2004-04-04 03:30:00 GMT"

x.1<-xts(1:7,as.POSIXct(x,format=fmt,tz="GMT"))
 x.1
#                    [,1]
#2004-01-01 01:15:00    1
#2004-01-01 01:30:00    2
#2004-01-01 01:45:00    3
#2004-01-01 02:00:00    4
#2004-01-01 02:30:00    5
#2004-01-01 03:00:00    6
#2004-01-01 03:15:00    7
#Warning message:
#timezone of object (GMT) is different than current timezone (). 
 y.1<-xts(1:6,as.POSIXct(y,format=fmt,tz="GMT"))
 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 (GMT) is different than current timezone (). 

 update(index(y.1),month=unique(month(index(x.1))))  
#[1] "2004-01-04 01:15:00 GMT" "2004-01-04 01:30:00 GMT"
#[3] "2004-01-04 01:45:00 GMT" "2004-01-04 02:00:00 GMT"
#[5] "2004-01-04 02:30:00 GMT" "2004-01-04 03:30:00 GMT"


#Here is where the problem occurs


index(y.1)<-update(index(y.1),month=unique(month(index(x.1))))  
y.1
#                    [,1]
#2004-01-03 20:15:00    1
#2004-01-03 20:30:00    2
#2004-01-03 20:45:00    3
#2004-01-03 21:00:00    4
#2004-01-03 21:30:00    5
#2004-01-03 22:30:00    6
#Warning message:
#timezone of object (GMT) is different than current timezone ().

#So I am going to change the timezone in the system and see what happens
Sys.setenv(TZ="GMT")

y.1<-xts(1:6,as.POSIXct(y,format=fmt,tz="GMT"))
update(index(y.1),month=unique(month(index(x.1))))  
#[1] "2004-01-04 01:15:00 GMT" "2004-01-04 01:30:00 GMT"
#[3] "2004-01-04 01:45:00 GMT" "2004-01-04 02:00:00 GMT"
#[5] "2004-01-04 02:30:00 GMT" "2004-01-04 03:30:00 GMT"
 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


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: Saturday, November 3, 2012 11:44 AM
Subject: Re: [R] override date in xts time series


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)))                   



More information about the R-help mailing list