From maechler at stat.math.ethz.ch  Thu Aug 21 10:05:01 2008
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Thu, 21 Aug 2008 10:05:01 +0200
Subject: [Rd] names of return value of median
In-Reply-To: <22D850BC39A25742977325ADDE208E7702669563C8@chiexchange02.w2k.jumptrading.com>
References: <22D850BC39A25742977325ADDE208E7702669563C8@chiexchange02.w2k.jumptrading.com>
Message-ID: <18605.8621.48809.920595@stat.math.ethz.ch>

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



[Rd] names of return value of median
Martin Maechler