Gregory Warnes
2005-Dec-12 19:03 UTC
[Rd] FW: Bug in sum() with named data frame arguments
I got an email error message when I attempted to send this from my work account. I have manually added it to the bug tracker, and am resending from my personal account. -G On 12/12/05, Warnes, Gregory R <gregory.r.warnes@pfizer.com> wrote:> > > > > -----Original Message----- > > From: Warnes, Gregory R > > Sent: Monday, December 12, 2005 1:53 PM > > To: 'bugs@cran.r-project.org' > > Subject: Bug in sum() with named data frame arguments > > > > > > Sum fails if it is passed a data frame argument with a name. It also > generated different error messages depending on exactly how the data frame > is passed in: > > > > > df <- data.frame(m1=1:2,m2=3:4) > > > df > > m1 m2 > > 1 1 3 > > 2 2 4 > > > sum(df) # succeeds > > [1] 10 > > > sum(df=df) # fails > > Error in as.matrix(x) : argument "x" is missing, with no default > > > sum(1,df=df) # fails, different error message > > Error in sum(..., na.rm = na.rm) : invalid 'mode' of argument > > > sum(df=df,1) # fails, different error message > > Error in sum.default(..., na.rm = na.rm) : > > invalid 'mode' of argument > > ## Note: no sum.default exists... > > > > > > No similar problem exists for matrixes: > > > > > x <- as.matrix(df) > > > sum(x) > > [1] 10 > > > sum(df=x) > > [1] 10 > > > sum(m1=x) > > [1] 10 > > > sum(1,m1=x) > > [1] 11 > > > sum(m1=x,1) > > [1] 11 > > > > > > > The cause seems to be that the generic sum is defined as: > > > > > sum > > function (..., na.rm = FALSE) > > .Internal(sum(..., na.rm = na.rm)) > > <environment: namespace:base> > > > > but the S3 methods are defined as > > > > > Summary.data.frame > > function (x, ...) > > { > > x <- as.matrix(x) > > if (!is.numeric(x) && !is.complex(x)) > > stop("only defined on a data frame with all numeric or complex > variables") > > NextMethod(.Generic) > > } > > <environment: namespace:base> > > > > I suspect that the proper resolution of this bug is to change the base > 'sum' method to provide a 1st argument named 'x', and to update the > documentation. > > > > Note that the current implementation also failes when more than one data > frame argument is supplied: > > > > > sum(y,y,y) > > Error in sum.default(..., na.rm = na.rm) : > > invalid 'mode' of argument > > > > while the same thing with matrixes works fine: > > > > > sum(y,y,y) > > Error in sum.default(..., na.rm = na.rm) : > > invalid 'mode' of argument > > > > -Greg > > > > Gregory R. Warnes, Ph.D. > > Associate Director, Non-Clinical Statistics > > Pfizer Global Research and Development > > > >[[alternative HTML version deleted]]
>>>>> "Greg" == Gregory Warnes <gregory.warnes at gmail.com> >>>>> on Mon, 12 Dec 2005 14:03:00 -0500 writes:Greg> I got an email error message when I attempted to Greg> send this from my work account. I have manually Greg> added it to the bug tracker, and am resending from Greg> my personal account. Greg> -G Greg> On 12/12/05, Warnes, Gregory R <gregory.r.warnes at pfizer.com> wrote: >> >> >> >> > -----Original Message----- >> > From: Warnes, Gregory R >> > Sent: Monday, December 12, 2005 1:53 PM >> > To: 'bugs at cran.r-project.org' ^^^^^^^^^^^^^^^^^^^^^^^ Can you tell where you took this address from? We'd very much like that R bug reports be sent to R-bugs at r-project.org (from where they are forwarded to the repository in Denmark, *after* having been virus- and spam-filtered). Martin Maechler, ETH ZUrich