joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
219 stars 71 forks source link

Better column names when unnamed objects passed to merge #248

Closed joshuaulrich closed 6 years ago

joshuaulrich commented 6 years ago

The column names of a merge() result can be monstrous if raw, unnamed objects are passed to merge(). This can occur when merge() is called via do.call(). For example:

data(sample_matrix)
x <- as.xts(sample_matrix)
d <- setNames(lapply(head(x, 2), function(x) { dimnames(x) <- NULL; x }), NULL)

# cbind.xts() calls merge.xts()
do.call(cbind, d)
           c.50.0397819115463..50.2304961977954.
2007-01-02                              50.03978
2007-01-03                              50.23050
           c.50.1177772350145..50.421876002125.
2007-01-02                             50.11778
2007-01-03                             50.42188
           c.49.9504146442813..50.2304961977954.
2007-01-02                              49.95041
2007-01-03                              50.23050
           c.50.1177772350145..50.3976663383861.
2007-01-02                              50.11778
2007-01-03                              50.39767

# zoo is similar
do.call(cbind, lapply(d, as.zoo))
           structure(c(50.0397819115463, 50.2304961977954), .Dim = c(2L, 
2007-01-02                                                       50.03978
2007-01-03                                                       50.23050
           structure(c(50.1177772350145, 50.421876002125), .Dim = c(2L, 
2007-01-02                                                      50.11778
2007-01-03                                                      50.42188
           structure(c(49.9504146442813, 50.2304961977954), .Dim = c(2L, 
2007-01-02                                                       49.95041
2007-01-03                                                       50.23050
           structure(c(50.1177772350145, 50.3976663383861), .Dim = c(2L, 
2007-01-02                                                       50.11778
2007-01-03                                                       50.39767

# matrix is a little nicer
do.call(cbind, lapply(d, as.matrix))
             X[[i]]   X[[i]]   X[[i]]   X[[i]]
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767