Jens Oehlschlägel-Akiyoshi
2000-Mar-08 09:16 UTC
[Rd] possible BUG with as.data.frame() and/or [.data.frame
Here is a possible BUG with as.data.frame() and/or [.data.frame which broke Michael Lapsleys RODBC-Code. Can anyone confirm it is a bug or a 'feature' of the prototype? tablename <- "abc" a <- as.data.frame(cbind("abc", 1:3)) b <- as.data.frame(cbind(tablename, 1:3)) # ok> aV1 V2 1 abc 1 2 abc 2 3 abc 3 # missing column name> btablename 1 abc 1 2 abc 2 3 abc 3 # subsetting ok> b[1,1][1] abc Levels: abc # subsetting fails> b[1,2]Error in [.data.frame(b, 1, 2) : undefined columns selected ##by contrast> data.frame(cbind(tablename, 1:3))tablename Var.2 1 abc 1 2 abc 2 3 abc 3 Regards -- Dr. Jens Oehlschlägel-Akiyoshi MD FACTORY GmbH Bayerstrasse 21 80335 München Tel.: 089 545 28-27 Fax.: 089 545 28-10 http://www.mdfactory.de -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2000-Mar-08 10:14 UTC
[Rd] possible BUG with as.data.frame() and/or [.data.frame
"Jens Oehlschlägel-Akiyoshi" <jens.oehlschlaegel-akiyoshi@mdfactory.de> writes:> Here is a possible BUG with as.data.frame() and/or [.data.frame which broke > Michael Lapsleys RODBC-Code. > Can anyone confirm it is a bug or a 'feature' of the prototype? > > > tablename <- "abc" > a <- as.data.frame(cbind("abc", 1:3)) > b <- as.data.frame(cbind(tablename, 1:3)) > > # ok > > a > V1 V2 > 1 abc 1 > 2 abc 2 > 3 abc 3 > > # missing column name > > b > tablename > 1 abc 1 > 2 abc 2 > 3 abc 3 > > # subsetting ok > > b[1,1] > [1] abc > Levels: abc > > # subsetting fails > > b[1,2] > Error in [.data.frame(b, 1, 2) : undefined columns selected > > > ##by contrast > > data.frame(cbind(tablename, 1:3)) > tablename Var.2 > 1 abc 1 > 2 abc 2 > 3 abc 3It's a prototype "feature"... ... Version 3.4 Release 1 for Sun SPARC, SunOS 5.3 : 1996 ...> tablename <- "abc" > b <- as.data.frame(cbind(tablename, 1:3)) > btablename 1 abc 1 2 abc 2 3 abc 3> b[,2]Error in "[.data.frame"(b, , 2): undefined columns selected Dumped This is a conspiracy of three things: 1) The deparse.level feature of cbind (which is unsettable, but the internals still behave as if deparse.level=1) which causes a deparsing of simple arguments (i.e. symbols) into colnames, leaving others blank. 2) as.data.frame.matrix expects that if colnames are present, they can just be copied to the names() of the result 3) "[.data.frame" explicitly checks whether a column name is blank after subsetting, and protests if it is. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian Ripley
2000-Mar-08 10:16 UTC
[Rd] possible BUG with as.data.frame() and/or [.data.frame
> From: "Jens Oehlschlägel-Akiyoshi" <jens.oehlschlaegel-akiyoshi@mdfactory.de> > Date: Wed, 8 Mar 2000 10:16:39 +0100 > > Here is a possible BUG with as.data.frame() and/or [.data.frame which broke > Michael Lapsleys RODBC-Code.> Can anyone confirm it is a bug or a 'feature' of the prototype?It's the same in S-PLUS 3.4, but different in 2000 and 5.1. In short, it seems a bug in Svr3 that have been fixed since in S-PLUS, but one could argue that it is intentional.> tablename <- "abc" > a <- as.data.frame(cbind("abc", 1:3)) > b <- as.data.frame(cbind(tablename, 1:3)) > > # ok > > a > V1 V2 > 1 abc 1 > 2 abc 2 > 3 abc 3 > > # missing column name > > b > tablename > 1 abc 1 > 2 abc 2 > 3 abc 3It's empty not missing.> names(b)[1] "tablename" "" Here 2000 and 5.1 give tablename X.2 1 abc 1 2 abc 2 3 abc 3 There's an easy fix here: as.data.frame.matrix can fixed by adding the two lines after collabs <- dn[[2]] if(any(empty <- nchar(collabs)==0)) collabs[empty] <- paste("V", 1:ncols, sep = "")[empty] I am inclined to regard this as a reported bug, and fix it in R-release. I am not sure I know what the optional argument in as.data.frame.matrix is supposed to do: it seems documented nowhere. On the other hand, the behaviour has been there for a long time undetected (in R).> # subsetting ok > > b[1,1] > [1] abc > Levels: abc > > # subsetting fails > > b[1,2] > Error in [.data.frame(b, 1, 2) : undefined columns selectedThat appears to be deliberate: you cannot select unnamed columns. As for the original code that triggers the problem: in sqlSave() in RODBC newname <- as.data.frame(cbind(NA, NA, tablename, as.matrix(names(dat)), 12, "varchar", 255, 255, NA, NA, 1, NA)) it is almost always a good idea to name the arguments to cbind. -- 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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._