[R-SIG-Finance] Naming conventions for merged XTS columns

Jeff Ryan jeff.a.ryan at gmail.com
Wed Oct 10 05:38:52 CEST 2012


To provide some additional clarity now that I've been able to look into it:

Firstly, merge on data.frames is quite different than merge in
xts/zoo.  merge in zoo/xts is cbind.  Which using your objects gets
you something different:

> cbind(as.data.frame(M), as.data.frame(N) )
           a a
1970-01-02 1 2
1970-01-03 2 1
1970-01-04 2 2

>From there, xts is indeed different than both matrix and zoo.  The
matrix class doesn't care about non-unique column names.  zoo appends
the object symbol to the column, though can behave differently
depending on options passed.

Most of these variations are too expensive for xts in general, but to
keep column names unique we employ the R function make.names( ..
unique=TRUE).  This happens in the C code technically, but when the
columns are named, the results you see are a result of this call.

What gets more interesting is when names are not defined.  You can
peruse below and see what happens in at least a few cases.

In general, xts tries to make sure you have unique column names.
Which is oddly consistent with the data.frame constructor itself:

data.frame(a=1,a=3)
  a a.1
1 1   3

So, if you have colnames defined, make.names(colnames(x),unique=TRUE)
is the behavior you can expect.  If not, it depends on how you call
the related bind functions:

> x
                    [,1]
1970-01-01 00:00:01    1
1970-01-01 00:00:02    2
1970-01-01 00:00:03    3

> merge(x,x,x)
                    x x.1 x.2
1970-01-01 00:00:01 1   1   1
1970-01-01 00:00:02 2   2   2
1970-01-01 00:00:03 3   3   3

> cbind(x,x,x)
                    ..1 ..2 ..3
1970-01-01 00:00:01   1   1   1
1970-01-01 00:00:02   2   2   2
1970-01-01 00:00:03   3   3   3

> do.call(merge,list(x,x,x))
                    X1.3 X1.3.1 X1.3.2
1970-01-01 00:00:01    1      1      1
1970-01-01 00:00:02    2      2      2
1970-01-01 00:00:03    3      3      3

HTH
Jeff

On Tue, Oct 9, 2012 at 7:52 PM, Worik Stanton <worik.stanton at gmail.com> wrote:
> When I merge two xts with the same column names a '.1' is appended...
>
> Where does this convention come from and can it be firmly relied on?
>
> Sorry if this is a general 'R' question... But merge acts differently
> for data.frames
>
> cheers
> Worik
>
>> M <- xts(trunc(3*runif(3)), seq(as.Date(1), as.Date(3), by=1))
>> M
>            [,1]
> 1970-01-02    0
> 1970-01-03    2
> 1970-01-04    1
>> colnames(M) <- 'a'
>> N <- xts(trunc(3*runif(3)), seq(as.Date(1), as.Date(3), by=1))
>> colnames(N) <- 'a'
>> merge(N,M)
>            a a.1
> 1970-01-02 2   0
> 1970-01-03 1   2
> 1970-01-04 2   1
>>
>
>> merge(as.data.frame(M), as.data.frame(N) )
>   a
> 1 1
> 2 2
> 3 2
>>
>
>
> --
> it does not matter                      I think that I shall never see
> how much I dig and dig                    A billboard lovely as a tree
> this hole just                      Indeed, unless the billboards fall
> keeps getting deeper                      I'll never see a tree at all
>
> _______________________________________________
> 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.



-- 
Jeffrey Ryan
jeffrey.ryan at lemnica.com

www.lemnica.com



More information about the R-SIG-Finance mailing list