Search code examples
rxtscbind

Using cbind on XTS object changes the dash (-) character in previous column names to a dot (.)


I have some R code that creates an XTS object, and then performs various cbind operations in the lifetime of that object. Some of my columns have names such as "adx-1". That is fine until another cbind() operation is performed. At that point, any columns with the "-" character are changes to a ".". So "adx-1" becomes "adx.1".

To reproduce:

x = xts(order.by=as.Date(c("2014-01-01","2014-01-02")))
x = cbind(x,c(1,2))
x
           ..2
2014-01-01   1
2014-01-02   2

colnames(x) = c("adx-1")
x
           adx-1
2014-01-01     1
2014-01-02     2

x = cbind(x,c(1,2))
x
           adx.1 ..2
2014-01-01     1   1
2014-01-02     2   2

It doesn't just do this with numbers either. It changes "test-text" to "test.text" as well. Multiple dashes are changed too. "test-text-two" is changed to "test.text.two".

Can someone please explain why this happens and, if possible, how to stop it from happening?

I can of course change my naming schemes, but it would be preferred if I didn't have to.

Thanks!


Solution

  • merge.xts converts the column names into syntactic names, which cannot contain -. According to ?Quotes:

     Identifiers consist of a sequence of letters, digits, the period
     ('.') and the underscore.  They must not start with a digit nor
     underscore, nor with a period followed by a digit.
    

    There is currently no way to alter this behavior.