[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 20:38:16 CET 2013


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 \ > in...{{dropped:23}}



More information about the R-help mailing list