[Rd] names of return value of median

Martin Maechler maechler at stat.math.ethz.ch
Thu Aug 21 10:05:01 CEST 2008


>>>>> "VO" == Vadim Organovich <vogranovich at jumptrading.com>
>>>>>     on Wed, 20 Aug 2008 12:30:38 -0500 writes:

    VO> Dear R-devel,
    VO> The median() function assigns a name, "NA", to its return value if the return value is NA and the input vector has names, otherwise the names attribute is NULL. This looks strange and inconsistent with the behavior of mean().

    VO> This inconsistency becomes a problem when median() is used inside user code that relies on consistent naming convention.

    VO> Thanks,
    VO> Vadim

    >> foo <- c(x=as.numeric(NA), y=as.numeric(NA), z=as.numeric(NA))
    >> names(mean(foo))
    VO> NULL
    >> names(median(foo))
    VO> [1] NA

    VO> ## no names in input
    >> foo <- rep(as.numeric(NA), 3)
    >> names(median(foo))
    VO> NULL

I agree that this is a slight blemish.
Note that it comes from this construct :

  > foo <- c(x=NA)
  > foo[FALSE]
  named numeric(0)
  > foo[FALSE][NA]
  <NA> 
    NA 

which seems "logical" if you ponder about it.

However, for the case of the median(), we do not return names
anyway, even in the case it might make sense,
e.g. I might expect

    median( c(a = 1, b=2, d=100))

to return  c(b = 2)
but it really simply returns (unnamed) 2.

So, I'm going to change median() to return unnamed also in these
border line cases.

Martin



More information about the R-devel mailing list