[Rd] Notes on bug reports 3229 and 3242 - as.matrix.data.fram e

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 11 22:35:41 CET 2005

On Fri, 11 Feb 2005, Gorjanc Gregor wrote:

> From: Liaw, Andy [mailto:andy_liaw at merck.com]
>> From: Gorjanc Gregor
>> ! Yes, I was not able to do it from my data. But bellow is one. It is
>> ! a stupid one, but it works. The problem is use of as.data.frame in
>> ! tmp1$L <- as.data.frame(tmp$L). This looks like to produce
>> a corrupted
>> ! data.frame. If I use just tmp1$L <- tmp$L, write.table and
>> ! as.matrix.data.frame works OK. I still think that mine proposal can
>> ! give benefit, since it works also on corrupted data frames.
>> data(warpbreaks)
>> tmp <- as.data.frame(tapply(breaks, list(wool, tension), mean))
>> tmp1 <- data.frame(level=rownames(tmp))
>> tmp1$L <- as.data.frame(tmp$L)
> Here's the problem that Brian is referring to:  Why do you make one variable
> in the data frame a data frame?  That's what caused problem in
> write.table()!
> ! I agree completely and as I have described up it is my fault that
> ! I have/had problems with as.matrix.data.frame by use of write.table.
> ! But I think that my proposal is nice, since as.matrix.data.frame would
> ! be more robust.

It is actually much less robust.  It would work for embedded data frames 
of one column, but you could have a list column with entries of different 
lengths. e.g.

X <- data.frame(x=1:2, y = I(list(a=1, b=3:4)))
> as.matrix(X)
   x y
a 1 1
b 2 Integer,2

With your fix, this becomes an error.  And I could replace those entries 
by data frames containing lists of dates ....

Note that in R-devel write.table does not convert data frames to matrices, 
so this does not arise.  We could treat your example specially, but surely 
it was an error that is better found out about than hushed up.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list