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
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.
Hi,
If you don't want to use library(lubridate):
This should also work.
?index(y.1)<-as.POSIXct(gsub("(.*\\-).*(\\-.*)","\\101\\2",index(y.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.