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

Patrick Perry pperry at stern.nyu.edu
Fri Dec 1 04:45:21 CET 2017

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))

#> Error in ncol(xj) : object 'xj' not found

The offending code is here, in the definition of as.matrix.data.frame 

     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").


More information about the R-devel mailing list