[R] bug in unsplit()? (PR#1843)
ripley@stats.ox.ac.uk
ripley@stats.ox.ac.uk
Sun, 28 Jul 2002 14:48:45 +0100 (BST)
On Sun, 28 Jul 2002 p.dalgaard@biostat.ku.dk wrote:
> 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.
Calling with separate factors is what is actually documented for
interaction. The code for interaction shows that passing the arguments as
a list also works, and has since Dec 98. I'll alter interaction.Rd.
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._