[R] plot two time series with different length and different starting point in one figure.
Yuan, Rebecca
rebecca.yuan at bankofamerica.com
Tue Jan 22 23:28:15 CET 2013
Hello Arun,
Thanks very much! In this way, it works! I convert both A and B to the same day of the month, and therefore there is no NA shown for different last business day and last calendar day of the month.
You are very help!
Cheers,
Rebecca
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Tuesday, January 22, 2013 5:06 PM
To: Yuan, Rebecca
Cc: R help
Subject: Re: [R] plot two time series with different length and different starting point in one figure.
HI Rebecca,
Try this:
dateA<-seq.Date(as.Date("28JAN2012",format="%d%B%Y"),as.Date("28DEC2012",format="%d%B%Y"),by="month")
dateB<-seq.Date(as.Date("30JAN2012",format="%d%B%Y"),as.Date("30DEC2012",format="%d%B%Y"),by="month")
set.seed(15)
A<-data.frame(dateA,value=cumsum(sample(1:50,12,replace=TRUE)))
set.seed(25)
B<-data.frame(dateB,value=cumsum(sample(1:72,12,replace=TRUE)))
B[,1]<-as.Date(gsub("\\d+$","28",B[,1]))
B[,1][duplicated(B[,1],fromLast=TRUE)]<-as.Date(gsub("(.*-).*(-.*)","\\102\\2",B[,1][duplicated(B[,1],fromLast=TRUE)])) #this step may not be needed in ur data. In the month of march, there were two values
library(xts)
Anew<-as.xts(A[,-1],order.by=A[,1])
Bnew<-as.xts(B[,-1],order.by=B[,1])
res<-merge(Anew,Bnew)
library(zoo)
plot.zoo(res)
A.K.
----- Original Message -----
From: "Yuan, Rebecca" <rebecca.yuan at bankofamerica.com>
To: 'arun' <smartpink111 at yahoo.com>
Cc:
Sent: Tuesday, January 22, 2013 3:53 PM
Subject: RE: [R] plot two time series with different length and different starting point in one figure.
Hello Arun,
I do not want to remove those NA values because they are the monthly data but recorded as the last calendar date in A and last business date in B.
I tried to use
raw_time <- substr(raw_time,3,9)
raw_time <- as.Date(raw_time,format="%d%B%Y")
to cutoff the date and leave the month and year in raw_time, and then convert it to a valid date type of data, but I failed.
Is there a way that I can present
2012-09-28 NA NA 5400726 14861715970 2012-09-30 5035606 14832837436 NA NA
into something like
2012-09-30 5035606 14832837436 5400726 14861715970
By converting 2012-09-28 to the last calendar date as of 2012-09-30 then B will be recorded at the last business date of the month, and will not have any NA values.
Dput() gives me
> dput(tail(res))
structure(c(121, NA, 111, 111, 120, 119, 309, NA, 313, 307, 30, 313, 130, 130, NA, 130, 130, 130, 309, 313, NA, 309, 310, 315), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "", tclass = "Date", tzone = "", index = structure(c(134, 134, 134, 135, 135, 135), tzone = "", tclass = "Date"), .Dim = c(6L, 4L), .Dimnames = list(NULL, c("raw_acct", "raw_baln", "raw_acct.1",
"raw_baln.1")))
Thanks very much!
Cheers,
Rebecca
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Tuesday, January 22, 2013 3:41 PM
To: Yuan, Rebecca
Cc: R help
Subject: Re: [R] plot two time series with different length and different starting point in one figure.
Hi Rebecca,
In the previous email,
res<-merge(Anew,Bnew)
head(res)
# Anew Bnew
#2012-01-01 181 NA
#2012-01-02 59 NA
#2012-01-03 290 NA
#2012-01-04 196 NA
#2012-01-05 111 NA
#2012-01-06 297 NA
plot.zoo(res) # removes the NA values from Bnew.. (if NA was present in Anew, I guess, it would remove that from plotting)
If you want to remove the NA rows:
use, na.omit() or complete.cases()? #as I did in the previous email.
Could you dput() an example dataset?
A.K.
----- Original Message -----
From: "Yuan, Rebecca" <rebecca.yuan at bankofamerica.com>
To: 'arun' <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, January 22, 2013 2:38 PM
Subject: RE: [R] plot two time series with different length and different starting point in one figure.
Hello Arun,
This would help me to get the date type of data. A new question comes out that since the dates are not exactly the same on two date sets, there are some NA values in the merged data set, such as
2012-09-28 NA NA 5400726 14861715970 2012-09-30 5035606 14832837436 NA NA
Does R have a function to convert the date to some format of Sep,2012, therefore when I merge those two, they will not have those NA numbers...
Thanks,
Rebecca
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Tuesday, January 22, 2013 2:15 PM
To: Yuan, Rebecca
Cc: R help
Subject: Re: [R] plot two time series with different length and different starting point in one figure.
Hi Rebecca,
Assuming that 'raw_data' is data.frame with first column as "raw_time:
You could convert the raw_time to date format by
as.Date("28FEB2002",format="%d%B%Y")
#[1] "2002-02-28"
In your data, it should be:
raw_data$raw_time<- as.Date(raw_time,format="%d%B%Y")
Could you just dput() a few lines of your dataset if this is not working?
Tx.
A.K.
----- Original Message -----
From: "Yuan, Rebecca" <rebecca.yuan at bankofamerica.com>
To: 'arun' <smartpink111 at yahoo.com>
Cc:
Sent: Tuesday, January 22, 2013 2:08 PM
Subject: RE: [R] plot two time series with different length and different starting point in one figure.
Hello Arun,
My data shows that I do not have a date type of data:
summary(raw_data)
raw_time raw_acct raw_baln
28FEB2002: 1 Min. : 61714 Min. :117079835
28FEB2003: 1 1st Qu.: 75587 1st Qu.:158035150
28FEB2005: 1 Median :100234 Median :206906298
28FEB2006: 1 Mean : 96058 Mean :210550369
28FEB2007: 1 3rd Qu.:116908 3rd Qu.:263623782
28FEB2009: 1 Max. :121853 Max. :325290870
(Other) :127
>
How could I transfer the "raw_time" column to a date format, such as
summary(dateA)
Min. 1st Qu. Median Mean 3rd Qu. Max.
"2012-01-01" "2012-04-01" "2012-07-01" "2012-07-01" "2012-09-30" "2012-12-31"
Thanks very much!
Cheers,
Rebecca
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Tuesday, January 22, 2013 12:39 PM
To: Yuan, Rebecca
Cc: R help; Petr PIKAL
Subject: Re: [R] plot two time series with different length and different starting point in one figure.
Hi,
You could also try this:
dateA<-seq.Date(as.Date("1jan2012",format="%d%b%Y"),as.Date("31Dec2012",format="%d%b%Y"),by="day")
dateB<-seq.Date(as.Date("1Mar2012",format="%d%b%Y"),as.Date("30Nov2012",format="%d%b%Y"),by="day")
set.seed(15)
A<-data.frame(dateA,value=sample(1:300,366,replace=TRUE))
set.seed(25)
B<-data.frame(dateB,value=sample(1:300,275,replace=TRUE))
library(xts)
Anew<-as.xts(A[,-1],order.by=A[,1])
Bnew<-as.xts(B[,-1],order.by=B[,1])
res<-merge(Anew,Bnew)
res1<-res[complete.cases(res),]
library(zoo)
plot.zoo(res1)
plot.zoo(res)
A.K.
----- Original Message -----
From: "Yuan, Rebecca" <rebecca.yuan at bankofamerica.com>
To: 'PIKAL Petr' <petr.pikal at precheza.cz>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, January 22, 2013 10:36 AM
Subject: Re: [R] plot two time series with different length and different starting point in one figure.
Hello Petr,
As the time series have the same column names, I got the error message like:
--------------------------------------------------------------------
> m1<-merge(A, B, by.x = "time", by.y = "balance")
Error in fix.by(by.x, x) : 'by' must specify uniquely valid column(s)
--------------------------------------------------------------------
To plot A and B in one plot is to compare the difference between them...
Any other thoughts?
Thanks,
Rebecca
-----Original Message-----
From: PIKAL Petr [mailto:petr.pikal at precheza.cz]
Sent: Tuesday, January 22, 2013 10:28 AM
To: Yuan, Rebecca; R help
Subject: RE: plot two time series with different length and different starting point in one figure.
Hi
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Yuan, Rebecca
> Sent: Tuesday, January 22, 2013 4:07 PM
> To: R help
> Subject: [R] plot two time series with different length and different
> starting point in one figure.
>
> Hello,
>
> I do have two different time series A and B, they are different in
> length and starting point. A starts in Jan, 2012 and ends in Dec, 2012
> and B starts in March, 2012 and ends in Nov, 2012.
>
> How can I plot those two series A and B in the same plot? I.E., from
> Jan. 2012 - Feb, 2012, it would have one data point from A and from
> Mar, 2012-Nov, 2012, it would have two data points from A and B, and
> in December 2012, it would have one data point from A.
Merge those 2 series.
?merge
Regards
Petr
>
> Thanks very much!
>
> Cheers,
>
> Rebecca
>
>
> ----------------------------------------------------------------------
> This message, and any attachments, is for the
> intended...{{dropped:13}}
______________________________________________
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.
----------------------------------------------------------------------
This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer. If you are not the intended recipient, please delete this message.
----------------------------------------------------------------------
This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer. If you are not the intended recipient, please delete this message.
----------------------------------------------------------------------
This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer. If you are not the intended recipient, please delete this message.
----------------------------------------------------------------------
This message, and any attachments, is for the intended recipient(s) only, may contain information that is privileged, confidential and/or proprietary and subject to important terms and conditions available at http://www.bankofamerica.com/emaildisclaimer. If you are not the intended recipient, please delete this message.
More information about the R-help
mailing list