[R] bug in stack?

apjaworski@mmm.com apjaworski at mmm.com
Tue Sep 23 20:59:04 CEST 2003


I am posting it here because I am not sure if the behavior described below
is actually a bug.

If I do something like this:

> x1 <- 1:3
> x2 <- 5:9
> x3 <- 21:27
> ll <- list(x1, x2, x3)
> stack(x1,x2,x3)

I get the following error:

Error in rep.int(names(x), lapply(x, length)) :
        invalid number of copies in "rep"

The problem seems to be that the generic list ll is lacking the names
attribute.

The stack.default function (in frametools.R) looks like this:

> stack.default
function (x, ...)
{
    x <- as.list(x)
    x <- x[unlist(lapply(x, is.vector))]
    data.frame(values = unlist(unname(x)), ind = factor(rep.int(names(x),
        lapply(x, length))))
}

and the last statement generates an error if names(x) evaluates to NULL.
If we add the following line of code before the last statement

if(is.null(names(x))) names(x) <- seq(along=x)

the stack function will work fine even for "nameless" lists.


Andy

__________________________________
Andy Jaworski
Engineering Systems Technology Center
3M Center, 518-1-01
St. Paul, MN 55144-1000
-----
E-mail: apjaworski at mmm.com
Tel:  (651) 733-6092
Fax:  (651) 736-3122




More information about the R-help mailing list