> From: [EMAIL PROTECTED] > > Hello, > I found a potential problem in R 2.1.0 (and R 2.0.1) > > I expect that > > > tmp <- FUN(x1, x2, x3, x4) > > as.data.frame(tmp) > > is the same as > > as.data.frame(FUN(x1, x2, x3, x4)) > > since the tmp variable in this case is unnecessary. > > However, below I will demonstrate that under an odd set of > conditions, I > can correctly perform as.data.frame(tmp), but not > as.data.frame(FUN(x1, > x2, x3, x4)). > > ## This code works correctly > FUN <- function(x1, x2, x3, x4) > cbind(x1[, 1, 1:2], x1[, 2, 1:2])[, 1] > > x1 <- array(1:9, c(3, 3, 3)) > tmp <- FUN(x1[1:3, , ], x2 = c("a", "b"), x3 = c("a", "b"), > x4 = c("a", > "b")) > > ## Works correctly > as.data.frame(tmp) > > tmp > 1 1 > 2 2 > 3 3 > > > ## This (supposedly equivalent) code gives an error > > as.data.frame(FUN(x1[1:3,,], x2 = c("a", "b"), x3 = c("a", "b"), x4 = > c("a", "b"))) > > Error in "names<-.default"(`*tmp*`, value = c("FUN(x1[1:3, , ], x2 = > c(\"a\", \"b\"), x3 = c(\"a\", \"b\"), x4 = c(\"a\", ", : > 'names' attribute [2] must be the same length as the vector [1] > > Note, that while the extra (unused) arguments in FUN seem unnecessary, > as well as the odd indexing, the problem disappears when I remove the > extraneous values. Unfortunately, I have not found a more > elegant way to > present this problem, but hopefully this code will be helpful.
The basic problem, I think, boils down to something like this: > f <- function(x) deparse(substitute(x)) > f(FUN(x1[1:3,,], x2=c("a","b"), x3=c("a", "b"), x4=c("a", "b"))) [1] "FUN(x1[1:3, , ], x2 = c(\"a\", \"b\"), x3 = c(\"a\", \"b\"), x4 = c(\"a\", " [2] " \"b\"))" which is caused by deparse() chopping up the expression. The fix would be to set the width.cutoff argument to something large. Here's a proposed patch: --- R-2.1.0/src/library/base/R/dataframe.R 2005-04-18 06:19:15.000000000 -0 400 +++ R-2.1.0-fix/src/library/base/R/dataframe.R 2005-04-22 23:17:18.972665600 -0 400 @@ -114,7 +114,7 @@ as.data.frame.vector <- function(x, row.names = NULL, optional = FALSE) { nrows <- length(x) - nm <- deparse(substitute(x)) + nm <- deparse(substitute(x), width.cutoff=500) if(is.null(row.names)) { if (nrows == 0) row.names <- character(0) @@ -235,7 +235,7 @@ as.data.frame.model.matrix(x, row.names, optional) else { # as.data.frame.vector without removing names nrows <- length(x) - nm <- deparse(substitute(x)) + nm <- deparse(substitute(x), width.cutoff=500) if(is.null(row.names)) { if (nrows == 0) row.names <- character(0) (I used width.cutoff=500, as ?deparse says that the max. I'd imagine the number of characters allowed for valid symbol names in R is probably lower than that?) Andy > Robert > > Robert McGehee > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396 Fax:617/476-6389 > mailto:[EMAIL PROTECTED] > > > > This e-mail, and any attachments hereto, are intended for > us...{{dropped}} > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > > ______________________________________________ R-devel@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-devel