diff --git a/R/seqapply.R b/R/seqapply.R index 1967de6f..07316ef4 100644 --- a/R/seqapply.R +++ b/R/seqapply.R @@ -21,6 +21,10 @@ setMethod("unsplit", "List", function(value, f, drop = FALSE) { if (NROW(value_flat) != length(f)) stop("Length of 'unlist(value)' must equal length of 'f'") splitAsList(value_flat, f, drop = drop) <- value + if (!is.null(names(value_flat))) { + nms <- relist(names(value_flat), value) + splitAsList(names(value_flat), f, drop = drop) <- nms + } value_flat }) diff --git a/inst/unitTests/test_seqapply.R b/inst/unitTests/test_seqapply.R index 36c30330..73aa0652 100644 --- a/inst/unitTests/test_seqapply.R +++ b/inst/unitTests/test_seqapply.R @@ -11,6 +11,15 @@ test_unsplit <- function() { v <- 1:5 l <- splitAsList(v, f) - checkIdentical(unsplit(l, Rle(f)), v) + checkIdentical(unsplit(l, Rle(f)), v) + + names(ir) <- letters[1:5] + rl <- split(ir, f) + checkIdentical(unsplit(rl, f), ir) + + ir <- IRanges(1:5, 11:15) + names(ir)[c(1,3,5)] <- letters[1:3] + rl <- split(ir, f) + checkIdentical(unsplit(rl, f), ir) }