relist() is broken when the skeleton is a list with empty list elements
Hi,
On 06/07/2013 03:08 PM, Herv? Pag?s wrote:
Hi, relist() is broken when the skeleton is a list with empty list elements:
> x <- list(1:3, integer(0), 11:14)
> relist(unlist(x), x)
[[1]] [1] 1 2 3 [[2]] [1] 11 3 [[3]] [1] 11 12 13 14 Hard to believe that such a bug has been around for 6 years (i.e. since the introduction of relist()) without ever being noticed.
This bug has actually been noticed. Just found out it was already reported by Michael Lawrence 2 years ago: https://stat.ethz.ch/pipermail/r-devel/attachments/20110811/4bbaa84f/attachment.pl Any chance it can be fixed? Below is the patch against current R-devel if that helps (it fixes relist.list and relist.matrix which are both broken in the same way). Thanks, H. hpages at thinkpad:~/svn/R/R-devel$ svn diff Index: src/library/utils/R/relist.R =================================================================== --- src/library/utils/R/relist.R (revision 63132) +++ src/library/utils/R/relist.R (working copy) @@ -119,13 +119,13 @@ relist.list <- function(flesh, skeleton=attr(flesh, "skeleton")) { - ind <- 1L + offset <- 0L result <- skeleton for (i in seq_along(skeleton)) { size <- length(unlist(result[[i]])) result[[i]] <- - relist(flesh[ind:(ind + size - 1L)], result[[i]]) - ind <- ind + size + relist(flesh[seq_len(size) + offset], result[[i]]) + offset <- offset + size } result } @@ -139,13 +139,13 @@ n <- nrow(skeleton) m <- ncol(skeleton) result <- skeleton - ind <- 1L - for (j in 1L:m) - for (i in 1L:n) { + offset <- 0L + for (j in seq_len(m)) + for (i in seq_len(n)) { size <- length(unlist(skeleton[[i, j]])) - result[[i, j]] <- relist(flesh[ind:(ind + size - 1)], + result[[i, j]] <- relist(flesh[seq_len(size) + offset], skeleton[[i, j]]) - ind <- ind + size + offset <- offset + size } result }
Cheers, H.
> sessionInfo()
R version 3.0.1 (2013-05-16) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base
Herv? Pag?s Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpages at fhcrc.org Phone: (206) 667-5791 Fax: (206) 667-1319