[R] bug in unsplit()? (PR#1843)

p.dalgaard@biostat.ku.dk p.dalgaard@biostat.ku.dk
Sun, 28 Jul 2002 12:27:17 +0200 (MET DST)


Hedderik van Rijn <hedderik@cmu.edu> writes:

> 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 

[Forwarded to r-bugs, since the docs say that f can be a list in all
the split-related functions]

Thanks for pointing this out. I think your code should work, although
I'm slightly uneasy about actually modifying f, so how about

        len <- length(if (is.list(f)) f[[1]] else f)
        x <- vector(mode = typeof(value[[1]]), length = len)

?

By the way, I see that split.default starts with 

    if (is.list(f)) 
        f <- factor(do.call("interaction", f))

What is the reason (if any) why this cannot just be f <-
interaction(f) ? If there is one, then it is likely that you would
have to do the same in unsplit.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._