From pgrosu at gmail.com  Thu Jan 14 23:35:49 2016
From: pgrosu at gmail.com (Paul Grosu)
Date: Thu, 14 Jan 2016 17:35:49 -0500
Subject: [Rd] as.data.frame and illegal row.names argument (bug
	in	package:DoE.wrapper?)
References: <CAF8bMcaQvSHxDbEkLexMOLRFWCPXa1DSvykE+9xeWnRudouknQ@mail.gmail.com>
Message-ID: <074701d14f1b$eb665ac0$c2331040$@gmail.com>


Hi Bill,

The thing is that is happening here is the specific instance of
as.data.frame that is being run, which in this instance switch between
as.data.frame.matrix() and as.data.frame.matrix().  I attached the
dataframe.R code, which you can find the src/library/base/R folder of the
source code.  Though if you use data.frame() it will give a more expected
result.  

For instance the first runs as follows through matrix:

> as.data.frame.matrix(matrix(1:6,nrow=3), row.names=c("One","Two"))
  V1 V2
1  1  4
2  2  5
3  3  6

The other two run via vector:

> as.data.frame.vector(1:3, row.names=c("One","Two"))
    1:3
One   1
Two   2
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

> as.data.frame.vector(c("a","b","c"), row.names=c("One","Two"))
    c("a", "b", "c")
One                a
Two                b
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

The thing is that if you use data.frame() it will work more as expected:

> data.frame(matrix(1:6,nrow=3), row.names=c("One","Two"))
Error in data.frame(matrix(1:6, nrow = 3), row.names = c("One", "Two")) : 
  row names supplied are of the wrong length

> data.frame(matrix(1:6,nrow=3), row.names=c("One","Two","Three"))
      X1 X2
One    1  4
Two    2  5
Three  3  6

> data.frame(c("a","b","c"), row.names=c("One","Two"))
Error in data.frame(c("a", "b", "c"), row.names = c("One", "Two")) : 
  row names supplied are of the wrong length

> data.frame(c("a","b","c"), row.names=c("One","Two","Three"))
      c..a....b....c..
One                  a
Two                  b
Three                c

> data.frame(1:3, row.names=c("One","Two"))
Error in data.frame(1:3, row.names = c("One", "Two")) : 
  row names supplied are of the wrong length

> data.frame(1:3, row.names=c("One","Two","Three"))
      X1.3
One      1
Two      2
Three    3

Hope it helps,
Paul

-----Original Message-----
From: R-devel [mailto:r-devel-bounces at r-project.org] On Behalf Of William
Dunlap via R-devel
Sent: Wednesday, January 13, 2016 4:46 PM
To: r-devel at r-project.org; Ulrike Groemping
Subject: [Rd] as.data.frame and illegal row.names argument (bug in
package:DoE.wrapper?)

as.data.frame methods behave inconsistently when they are given a row.name
argument of the wrong length.  The matrix method silently ignores row.names
if it has the wrong length and the numeric, integer, and character methods
do not bother to check and thus make an illegal data.frame.

> as.data.frame(matrix(1:6,nrow=3), row.names=c("One","Two"))
  V1 V2
1  1  4
2  2  5
3  3  6
> as.data.frame(1:3, row.names=c("One","Two"))
    1:3
One   1
Two   2
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs
> as.data.frame(c("a","b","c"), row.names=c("One","Two"))
    c("a", "b", "c")
One                a
Two                b
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

(The warnings are from the printing, not the making, of the data.frames.)

I ran into this while using the DoE.wrapper package, which has what I think
is a typo, giving "t" as the row.names for the output of mapply():
   cross.design.R:    ro <- as.data.frame(mapply("touter",ro1, ro2,
"paste", sep="_"),"t")

I don't know all the reasons why people use as.data.frame instead of
data.frame.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

	[[alternative HTML version deleted]]

______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] as.data.frame and illegal row.names argument (bug in package:DoE.wrapper?)
Paul Grosu