[R] override date in xts time series
arun
smartpink111 at yahoo.com
Sat Nov 3 18:03:47 CET 2012
Hi,
Sorry, I forgot to answer the second question.
txt<-paste("\\10",unique(month(index(x.1))),"\\2",sep="") #without the as.character() also should work
#because
str(paste("\\10",unique(month(index(x.1))),"\\2",sep="")) # it returns a character
# chr "\\101\\2"
#Here too:
str(paste(10,unique(month(index(x.1))),2,sep=""))
# chr "1012"
#According to the description in paste()
"Concatenate vectors after converting to character. "
as.POSIXct(gsub("(.*\\-).*(\\-.*)",txt,index(y.1)))
#[1] "2004-01-04 01:15:00 EST" "2004-01-04 01:30:00 EST"
#[3] "2004-01-04 01:45:00 EST" "2004-01-04 02:00:00 EST"
#[5] "2004-01-04 02:30:00 EST" "2004-01-04 03:30:00 EST"
#Now, suppose if I want to change both the month and day from the original y.1
index(y.1)<-as.POSIXct(gsub("(.*\\-).*(\\-).*(\\s.*)","\\101\\207\\3",index(y.1))) #Here, the month will be 01 and day 07
y.1
# [,1]
#2004-01-07 01:15:00 1
#2004-01-07 01:30:00 2
#2004-01-07 01:45:00 3
#2004-01-07 02:00:00 4
#2004-01-07 02:30:00 5
#2004-01-07 03:30:00 6
Hope it 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: Saturday, November 3, 2012 12:32 PM
Subject: Re: [R] override date in xts time series
Sys.setenv(TZ="GMT") did
the trick! Thank you very much. I'll continue to work the larger
problem with this option.
Out of curiosity, however, can the following
code be modified so that the replacementargument is informed by the month
of x.1?:
index(y.1)<-as.POSIXct(gsub("(.*\\-).*(\\-.*)","\\101\\2",index(y.1)))
Something to the tune of the following
seems to work, but is it robust?:
txt<-paste("\\10",as.character(unique(month(index(x.1)))),"\\2",sep="")
index(y.1)<-as.POSIXct(gsub("(.*\\-).*(\\-.*)",txt,index(y.1)))
index(y.1)
# "2004-01-04 01:15:00 PST" "2004-01-04
01:30:00 PST"
# "2004-01-04 01:45:00 PST" "2004-01-04
02:00:00 PST"
# "2004-01-04 02:30:00 PST" "2004-01-04
03:30:00 PST"
What would the gsub 'pattern' string
be to replace the day, if I may ask? I'm not trying to push my luck,
but the gsub approach is new to me and don't quite follow everything that
is going on.
-Eric
More information about the R-help
mailing list