Robert.McGehee@geodecapital.com
2005-Apr-22 19:03 UTC
[Rd] as.data.frame: Error in "names<-.default" (PR#7808)
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. Robert Robert McGehee Geode Capital Management, LLC 53 State Street, 5th Floor | Boston, MA | 02109 Tel: 617/392-8396 Fax:617/476-6389 mailto:robert.mcgehee@geodecapital.com This e-mail, and any attachments hereto, are intended for us...{{dropped}}
> From: Robert.McGehee@geodecapital.com > > 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:robert.mcgehee@geodecapital.com > > > > 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 > > >
andy_liaw@merck.com
2005-Apr-24 01:00 UTC
[Rd] as.data.frame: Error in "names<-.default" (PR#7808)
> From: Robert.McGehee@geodecapital.com > > 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:robert.mcgehee@geodecapital.com > > > > 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 > > >
ripley@stats.ox.ac.uk
2005-Apr-27 19:21 UTC
[Rd] as.data.frame: Error in "names<-.default" (PR#7808)
On Sun, 24 Apr 2005 andy_liaw@merck.com wrote: [...]>> 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:[...]> (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?)This is an expression, and can be arbitrarily long. So one needs to do things like terms.formula: else paste(deparse(form[[2]]), collapse = "") or just use the initial part (as done elsewhere). It's fixed in R-patched now. -- Brian D. Ripley, ripley@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