[R] as.data.frame.array

Wollkind, Steven Steven.Wollkind at GeodeCapital.com
Wed Nov 14 16:49:05 CET 2007


I've noticed what seems like inconsistent behavior from this function,
but I can't figure out if this is intentional or not.

If an array has only one element, the names attached to that array are
not transferred to the data frame that is returned, while If the array
has two or more elements the names are preserved.  The documentation for
as.data.frame indicates that if the row.names argument is NULL then the
names attached to the object in question will be used for row names of
the resulting data frame.

Some sample code to demonstrate this behavior:

a <- array(c(1))
names(a) <- c("A")

b <- array(c(1,2))
names(b) <- c("A","B")

as.data.frame(a)
as.data.frame(b)


The code for as.data.frame.array reveals that drop is being called on
the array and then it is passed on to as.data.frame.vector.  It looks
like drop strips the names off of the array that is being passed, so it
seems like we should be checking if the row.names argument is null and
fetching those names before casting them aside.

Here's the code for as.data.frame.array:

function (x, row.names = NULL, optional = FALSE, ...) 
{
    d <- dim(x)
    if (length(d) == 1L) {
        value <- as.data.frame.vector(drop(x), row.names, optional, 
            ...)
        if (!optional) 
            names(value) <- deparse(substitute(x))[[1L]]
        value
    }
    else if (length(d) == 2L) {
        as.data.frame.matrix(x, row.names, optional, ...)
    }
    else {
        dn <- dimnames(x)
        dim(x) <- c(d[1L], prod(d[-1]))
        if (!is.null(dn)) {
            if (length(dn[[1L]])) 
                rownames(x) <- dn[[1L]]
            for (i in 2L:length(d)) if (is.null(dn[[i]])) 
                dn[[i]] <- seq_len(d[i])
            colnames(x) <- interaction(expand.grid(dn[-1]))
        }
        as.data.frame.matrix(x, row.names, optional, ...)
    }
}


Can someone tell me if there's a good reason for this behavior, or if
this is a bug in as.data.frame.array?  Also, is this the sort of
question that belongs on R devel rather than R help?  I'm only signed on
this list at present.

Thanks
Steve


Steve Wollkind
Associate Analyst
Geode Capital Management, LLC
1 Post Office Square / 28th Floor / Boston, MA 02109
steven.wollkind at geodecapital.com
Tel:   (617) 392-8991
Fax:  (617) 476-6389

This e-mail, and any attachments hereto, are intended fo...{{dropped:11}}



More information about the R-help mailing list