Bug is as.matrix.data.frame with nested data.frame

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Bug is as.matrix.data.frame with nested data.frame

Patrick Perry-2
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):

     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). Also, the code would be more
generic if it used is.data.frame here instead of inherits(, "data.frame").


Patrick

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Bug is as.matrix.data.frame with nested data.frame

Martin Maechler
>>>>> Patrick Perry <[hidden email]>
>>>>>     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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel