[Rd] Bug is as.matrix.data.frame with nested data.frame

Martin Maechler maechler at stat.math.ethz.ch
Fri Dec 1 10:00:34 CET 2017


>>>>> Patrick Perry <pperry at stern.nyu.edu>
>>>>>     on Thu, 30 Nov 2017 22:45:21 -0500 writes:

    > Converting a data.frame with a nested data.frame to a matrix fails:
    > x <- structure(list(a = data.frame(letters)),
    > class = "data.frame",
    > row.names = .set_row_names(26))

    > as.matrix(x)
    > #> Error in ncol(xj) : object 'xj' not found

    > The offending code is here, in the definition of as.matrix.data.frame 
    > (source/base/all.R):
    ^^^^^^^^^^^^^^^^^^^^^^ that's *not* file from the sources of R !

    > for (j in pseq) {
    >    if (inherits(X[[j]], "data.frame") && ncol(xj) > 1L)
    >        X[[j]] <- as.matrix(X[[j]])
    >    xj <- X[[j]]

    > It should be ncol(X[[j]]), not ncol(xj). 

yes (or something similar ..)
Interesting, that this has not been detected earlier.

[ OTOH, who nests data.frames inside data.frames? ]

If I fix the above, there's a next "bug" preventing
as.matrix(x)  to work for your case.

I'll look into it.
 
   > Also, the code would be more 
    > generic if it used is.data.frame here instead of inherits(, "data.frame").

No, that is not true as is.data.frame() is not generic, and base
functions call base, so there can't be a difference.

Martin



More information about the R-devel mailing list