[R] bug in unsplit()?

Hedderik van Rijn hedderik at cmu.edu
Sun Jul 28 04:42:23 CEST 2002


If the second argument to unsplit is not a simple vector (but a "list
containing multiple lists"), the function seems to have some problems.

Given a slight modification of the examples in help(split):

> xg <- split(x,list(g1=g,g2=g))
> unsplit(xg,list(g1=g,g2=g))
[1] -0.7877109  2.1757667
Warning messages: 
1: argument lengths differ in: split(x, f) 
2: number of items to replace is not a multiple of replacement length 

It seems to have problems finding the correct length of f. The following
simple addition to the unsplit code probably introduces more bugs than
it solves, but it worked for me. :-)

unsplit.new <- function (value, f) 
{
  if (is.list(f)) {
    f <- interaction(f)
  } 

  x <- vector(mode = typeof(value[[1]]), length = length(f))
  split(x, f) <- value
  x
}

> table(x==unsplit.new(xg,list(g1=g,g2=g)))

TRUE 
1000 

 - Hedderik.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list