From Martin Maechler <maechler@stat.math.ethz.ch>  Tue Sep 26 18:51:02 2000
From: Martin Maechler <maechler@stat.math.ethz.ch> (Martin Maechler)
Date: Tue, 26 Sep 2000 19:51:02 +0200
Subject: [Rd] bug in apply with median
In-Reply-To: <x2snqrrwch.fsf@blueberry.kubism.ku.dk>
References: <39CCB4C0.9F104660@metronet.com>
 <x2snqrrwch.fsf@blueberry.kubism.ku.dk>
Message-ID: <14800.57862.150376.54471@gargle.gargle.HOWL>

>>>>> "PD" == Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk> writes:
(Sep 23):

    PD> Larry Ammann <ammann@metronet.com> writes:
    >> I have found a problem in R version 1.1.1 when using apply with the
    >> median function.
    >> The problem can be illustrated with the following data matrix:
    >> 
    >> X1  X2  X3
    >> 1     2     3
    >> 4    5     6
    >> 7    8    NA
    >> 
    >> Enter this data matrix as X and then try
    >> apply(X,2,median,na.rm=T)
    >> The problem here is that the median function returns a named scalar if
    >> the number of
    >> observations is odd, but returns an unnamed scalar if the number of
    >> observations is
    >> even. This confuses the apply function in this case at:

    PD> for ( i in 1:1000 ) cat("Data frames are not matrices!\n")

                                 ==============================
    >> X<-matrix(c(1:8,NA),3,3)
    >> X
    PD> [,1] [,2] [,3]
    PD> [1,]    1    4    7
    PD> [2,]    2    5    8
    PD> [3,]    3    6   NA
    >> apply(X,2,median,na.rm=T)
    PD> [1] 2.0 5.0 7.5

    PD> Works fine.

    PD> However,

    >> Z<-data.frame(X)
    >> apply(Z,2,median,na.rm=T)
    PD> Error in names(x) == ans.names : comparison (1) is possible only for vector types

    PD> Question is whether this is a bug. Apply() never promised to work on
    PD> non-arrays.

I think we should 
  either   make it promiss and do this
  or	   make it produce a warning    when used with data.frames

The above problem is too common for non-experts to stumble on..


    PD> When applied columnwise, it would anyway be more obvious to use

    >> sapply(Z,median,na.rm=T)
    PD> X1  X2  X3 
    PD> 2.0 5.0 7.5 

This could be what   
     apply(<dataframe>, 2, * )   
should do
whereas we could have
	apply(dfr, 1, *)  === apply(data.matrix(dfr), 1, *)
when dfr is a data.frame.

Opinions ?
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] bug in apply with median
Martin Maechler