[R-SIG-Finance] [xts] merge function weird behaviour

Anass Mouhsine anass.mouhsine at gmail.com
Fri Dec 10 10:03:41 CET 2010


Well Murali,
I constructed the xts data series from 1-minute-bar OHLC series using 
the to.daily function from quantmod package.
And I took the close of the daily series.

 > str(t1)
An ‘xts’ object from 2007-01-02 to 2007-01-08 containing:
Data: num [1:6, 1] 97.9 97.3 96 96 95.7 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "Close"
Indexed by objects of class: [Date] TZ: America/New_York
Original class: 'xts'
xts Attributes:
NULL
 > str(t2)
An ‘xts’ object from 2007-01-02 to 2007-01-08 containing:
Data: num [1:6, 1] 94.6 94.4 92.5 92.4 92.2 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "Close"
Indexed by objects of class: [Date] TZ: America/New_York
Original class: 'xts'
xts Attributes:
NULL

#just to be sure
 > str(index(head(t1)))
Class 'Date' num [1:6] 13515 13516 13517 13518 13520 ...
 > str(index(head(t2)))
Class 'Date' num [1:6] 13515 13516 13517 13518 13520 ...

I will prepare some files to attach for tests in a following mail.

On 10/12/2010 09:30, Murali.Menon at avivainvestors.com wrote:
> It might be that the indices of the two series are not really the same. Try doing str(t1) and str(t2) and see what the index values are. They might appear to be Dates, but are actually internally some other representation.
>
> But really you need to send a reproducible example of the variables. How did you generate t1 and t2? With the text you've sent, it's not possible to reproduce the problem.
>
> Murali
>
> -----Original Message-----
> From: r-sig-finance-bounces at r-project.org [mailto:r-sig-finance-bounces at r-project.org] On Behalf Of Anass Mouhsine
> Sent: 10 December 2010 08:12
> To: r-sig-finance at r-project.org
> Subject: [R-SIG-Finance] [xts] merge function weird behaviour
>
> Hi all,
>
> I encountered a weird behaviour of merge function while using it with
> xts objects.
> I have multiple xts objects (10 timeseries) that I want to merge in
> order to conduct a multivariate analysis.
> Here is an example with only two timeseries
>
> #Let us define the time series
>   >  t1
>                                          Close
> 2007-01-02                  97.87
> 2007-01-03                  97.32
> 2007-01-04                  96.04
> 2007-01-05                  95.97
> 2007-01-07                  95.69
> 2007-01-08                  96.23
>   >  t2
>                                          Close
> 2007-01-02                  94.59
> 2007-01-03                  94.38
> 2007-01-04                  92.55
> 2007-01-05                  92.37
> 2007-01-07                  92.23
> 2007-01-08                  92.89
>   >  c(class(t1),class(t2))
> [1] "xts" "zoo" "xts" "zoo"
>
> #I do the merge using merge.xts
>   >  t<-merge(t1,t2)
>   >  t
>                                          Close                 Close
> 2007-01-02                  97.87                     NA
> 2007-01-02                     NA                  94.59
> 2007-01-03                  97.32                  94.38
> 2007-01-04                  96.04                  92.55
> 2007-01-05                  95.97                     NA
> 2007-01-05                     NA                  92.37
> 2007-01-07                  95.69                     NA
> 2007-01-07                     NA                  92.23
> 2007-01-08                  96.23                  92.89
>
> # I don't see where the problem lays since both xts objects have the
> same index
>   >  index(t1)
> [1] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-07"
> "2007-01-08"
>   >  index(t2)
> [1] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-07"
> "2007-01-08"
>
> # I tried using join but with no satisfying result
>   >t<-merge(head(t1),head(t2),join='inner')
>   >  t
>              CHFJPY..2007.....Close AUDJPY..2007.....Close
> 2007-01-03                  97.32                  94.38
> 2007-01-04                  96.04                  92.55
> 2007-01-08                  96.23                  92.89
>
> I am really out of tricks here :-[ .
>
> So if someone is kind enough as to point me towards the right direction,
> it would make my day.
>
> Thx in advance,
>
> Anass
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list