Skip to content

Commit

Permalink
Shorten column names for merge on unnamed objects
Browse files Browse the repository at this point in the history
The column names of a merge() result can be monstrous for raw, unnamed
numeric objects. Something similar to dput() for the entire column is
used, but zoo deparses the dput() output and limits the deparse length
to ~60 characters.

Use a modified version of makeNames() (defined in merge.zoo()) to make
the column names. Major differences are:
  - Use substitute(alist(...)) to avoid evaluating '...'
  - Use deparse(x, nlines = 1L) to deparse only the first expression

NOTE: this may break existing code that relies on behavior for integer
objects.

For example, current behavior is:

  x <- .xts(1:5, 1:5)
  lx <- list(x, x)
  do.call(merge, lx)
                      X1.5 X1.5.1
  1969-12-31 18:00:01    1      1
  1969-12-31 18:00:02    2      2
  1969-12-31 18:00:03    3      3
  1969-12-31 18:00:04    4      4
  1969-12-31 18:00:05    5      5

New behavior has column names:

  structure.1.5...Dim...c.5L..1L...index...structure.1.5..tclass...c..POSIXct...
  structure.1.5...Dim...c.5L..1L...index...structure.1.5..tclass...c..          POSIXct....1

Fixes #248. See #259.
  • Loading branch information
joshuaulrich committed Nov 3, 2018
1 parent 02fe358 commit 7c2809f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Changed in xts 0.11-2:

o Make column names for merge() results with unnamed objects shorter and more
like zoo (#248). This also makes na.fill() much faster (#259).
BREAKING: This may break existing code for integer unnamed objects.

o Fix subset when 'index(x)' and 'i' contain duplicates. Thanks to Stack
Overflow user 'scs' (https://stackoverflow.com/users/4024268/scs) for the
report, and Philippe Verspeelt for debugging (#275).
Expand Down
14 changes: 13 additions & 1 deletion R/merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,19 @@ merge.xts <- function(...,
if(is.null(suffixes)) {
syms <- names(dots)
syms[nchar(syms)==0] <- as.character(dots)[nchar(syms)==0]
if(is.null(syms)) syms <- as.character(dots)
if(is.null(syms)) {
# Based on makeNames() in merge.zoo()
syms <- substitute(alist(...))[-1L]
nm <- names(syms)
fixup <- if (is.null(nm)) seq_along(syms) else !nzchar(nm)
dep <- sapply(syms[fixup], function(x) deparse(x, nlines = 1L))
if(is.null(nm)) {
nm <- dep
} else if(any(fixup)) {
nm[fixup] <- dep
}
syms <- nm
}
} else
if(length(suffixes) != length(dots)) {
warning("length of suffixes and does not match number of merged objects")
Expand Down
12 changes: 12 additions & 0 deletions inst/unitTests/runit.merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,15 @@ test.n_way_merge_on_all_types <- function() {
checkIdentical(m, m3)
}
}

test.shorter_colnames_for_unnamed_args <- function() {
X <- .xts(rnorm(10, 10), 1:10)

types <- c("double", "integer", "logical", "character", "complex")
for (type in types) {
x <- X
storage.mode(x) <- type
mx <- do.call(merge, list(x, x))
checkTrue(all(nchar(colnames(mx)) < 200), type)
}
}

0 comments on commit 7c2809f

Please sign in to comment.