Re: [Rd] FW: Bug in sum() with named data frame arguments

From: Gregory Warnes <gregory.warnes_at_gmail.com>
Date: Mon 12 Dec 2005 - 19:03:00 GMT

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]]



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue Dec 13 06:14:34 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:34 GMT