[R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread Yuan, Rebecca
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.

Thanks very much!

Cheers,

Rebecca


--
This message, and any attachments, is for the intended r...{{dropped:5}}

__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread PIKAL Petr
Hi

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@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
 r...{{dropped:5}}
 
 __
 R-help@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.

__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread Yuan, Rebecca
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.pi...@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-boun...@r-project.org [mailto:r-help-bounces@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@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread David Winsemius

On Jan 22, 2013, at 7:07 AM, Yuan, Rebecca wrote:

 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.

You could set the xlim argument to c( min(timeA, timeB), max(timeA, timeB) ) in 
the `plot` of either of the series and then use `lines` for the other series, 
perhaps with a different color argument.

-- 

David Winsemius
Alameda, CA, USA

__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread Yuan, Rebecca
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  NA5400726 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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@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:smartpink...@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.y...@bankofamerica.com
To: 'PIKAL Petr' petr.pi...@precheza.cz
Cc: R help r-help@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.pi...@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-boun...@r-project.org [mailto:r-help-bounces@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}}

__
R-help@r-project.org mailing list

Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread Yuan, Rebecca
Hello David,

If I use plot with the following code:

plot(A, type = o, col = plot_colors[plotcolor], axes = FALSE, 
ann = FALSE)
par(new=TRUE)
plot(B, type = o, col = plot_colors[plotcolor+1], axes = 
FALSE, ann = FALSE)
box()

I will have the two series in one plot, but they are only from March,2012 to 
Nov, 2012, the nonoverlapping months are dropped out...

I know in Matlab that I can specify the x axis such as 

Plot(timeofA, A)
Hold on;
Plot(timeofB, B)

to get them in the same figure, but in R, I do not know how to do it.

Thanks,

Rebecca

-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net] 
Sent: Tuesday, January 22, 2013 2:34 PM
To: Yuan, Rebecca
Cc: R help
Subject: Re: [R] plot two time series with different length and different 
starting point in one figure.


On Jan 22, 2013, at 7:07 AM, Yuan, Rebecca wrote:

 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.

You could set the xlim argument to c( min(timeA, timeB), max(timeA, timeB) ) in 
the `plot` of either of the series and then use `lines` for the other series, 
perhaps with a different color argument.

-- 

David Winsemius
Alameda, CA, USA

--
This message, and any attachments, is for the intended r...{{dropped:2}}

__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread David Winsemius

On Jan 22, 2013, at 11:42 AM, Yuan, Rebecca wrote:

 Hello David,
 
 If I use plot with the following code:
 
   plot(A, type = o, col = plot_colors[plotcolor], axes = FALSE, 
 ann = FALSE)
   par(new=TRUE)
   plot(B, type = o, col = plot_colors[plotcolor+1], axes = 
 FALSE, ann = FALSE)
   box()
 
 I will have the two series in one plot, but they are only from March,2012 to 
 Nov, 2012, the nonoverlapping months are dropped out...
 
 I know in Matlab that I can specify the x axis such as 
 
 Plot(timeofA, A)
 Hold on;
 Plot(timeofB, B)
 
 to get them in the same figure, but in R, I do not know how to do it.

As I said before . You need to use the xlim argument to 'plot'. If you 
insist on using plot twice then you will need to use 'xlim=' twice, although 
I thought it would be easier to use `plo`t first and `lines` second.

-- 
David.
 
 Thanks,
 
 Rebecca
 
 -Original Message-
 From: David Winsemius [mailto:dwinsem...@comcast.net] 
 Sent: Tuesday, January 22, 2013 2:34 PM
 To: Yuan, Rebecca
 Cc: R help
 Subject: Re: [R] plot two time series with different length and different 
 starting point in one figure.
 
 
 On Jan 22, 2013, at 7:07 AM, Yuan, Rebecca wrote:
 
 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.
 
 You could set the xlim argument to c( min(timeA, timeB), max(timeA, timeB) ) 
 in the `plot` of either of the series and then use `lines` for the other 
 series, perhaps with a different color argument.
 
 -- 
 
 David Winsemius
 Alameda, CA, USA
 
 --
 This message, and any attachments, is for the intended...{{dropped:9}}

__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread arun
Hi,

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))
res-merge(A,B,by.x=dateA,by.y=dateB) #it works


A.K.



- Original Message -
From: Yuan, Rebecca rebecca.y...@bankofamerica.com
To: 'PIKAL Petr' petr.pi...@precheza.cz
Cc: R help r-help@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.pi...@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-boun...@r-project.org [mailto:r-help-bounces@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@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.


__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread arun
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.y...@bankofamerica.com
To: 'PIKAL Petr' petr.pi...@precheza.cz
Cc: R help r-help@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.pi...@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-boun...@r-project.org [mailto:r-help-bounces@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@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.


__
R-help@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.


Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread arun
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.y...@bankofamerica.com
To: 'arun' smartpink...@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:smartpink...@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.y...@bankofamerica.com
To: 'PIKAL Petr' petr.pi...@precheza.cz
Cc: R help r-help@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.pi...@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-boun...@r-project.org [mailto:r-help-bounces@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@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.


__
R-help@r-project.org mailing list

Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread arun
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.y...@bankofamerica.com
To: 'arun' smartpink...@yahoo.com
Cc: R help r-help@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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@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:smartpink...@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.y...@bankofamerica.com
To: 'PIKAL Petr' petr.pi...@precheza.cz
Cc: R help r-help@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.pi...@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-boun...@r-project.org [mailto:r-help-bounces@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 

Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread arun
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.y...@bankofamerica.com
To: 'arun' smartpink...@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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@yahoo.com
Cc: R help r-help@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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@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 

Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread Yuan, Rebecca
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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@yahoo.com
Cc: R help r-help@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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@yahoo.com
Cc: 
Sent: Tuesday, 

Re: [R] plot two time series with different length and different starting point in one figure.

2013-01-22 Thread arun
Hi Rebecca,
No problem.
Just a doubt regarding the last calendar day and last business day.
dateA-seq(as.Date(01FEB2012,format=%d%B%Y),length=15,by=1 month)-1   
#gives the last calendar day/month
dateB- 
seq.Date(as.Date(28MAR2012,format=%d%B%Y),as.Date(28DEC2012,format=%d%B%Y),by=month)
 #here I used day 28.  If it didn't change
#then this works.  
set.seed(15)
 A-data.frame(dateA,value=cumsum(sample(1:50,15,replace=TRUE)))
 set.seed(25)
 B-data.frame(dateB,value=cumsum(sample(1:72,10,replace=TRUE)))
 A[,1]-as.Date(gsub(\\d+$,28,A[,1]))
library(xts)
library(zoo)
 Anew-as.xts(A[,-1],order.by=A[,1])
  Bnew-as.xts(B[,-1],order.by=B[,1])
  res-merge(Anew,Bnew)
 plot.zoo(res)
 
From your reply, it seems like dateB day didn't change.

A.K.





- Original Message -
From: Yuan, Rebecca rebecca.y...@bankofamerica.com
To: 'arun' smartpink...@yahoo.com
Cc: R help r-help@r-project.org
Sent: Tuesday, January 22, 2013 5:28 PM
Subject: RE: [R] plot two time series with different length and different 
starting point in one figure.

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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@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:smartpink...@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.y...@bankofamerica.com
To: 'arun' smartpink...@yahoo.com
Cc: R help r-help@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