Re: [Rd] names of return value of median

From: Martin Maechler <>
Date: Thu, 21 Aug 2008 10:05:01 +0200

>>>>> "VO" == Vadim Organovich <> >>>>> 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]

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 mailing list Received on Thu 21 Aug 2008 - 08:07:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Aug 2008 - 09:37:05 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive