Skip to content

which function

3 messages · Gafar Matanmi Oyeyemi, Jeff Newmiller, Jim Lemon

#
Dear,
I need help on which.min and  which.max. functions. Is there a function to
fetch me the point that in vector that gives median value such like the
commands for minimum and maximum values.
Thanks.
#
The median is not always a member of the data set. What do you really want? 

I for one would want people to follow the guidance in the footer on every email on this mailing list.
#
Hi Gafar,
As Jeff has pointed out, the median value may not exist within the
dataset. However, this function will give you either the position of
the value that is the median, or the position of the two closest
values if none equal the median. Be aware that this function may fall
victim to the "indistinguishable difference" problem (FAQ 7.31).

which.median<-function(x) {
 xmed<-median(x)
 if(any(xmed==x)) {
  wmed<-which(xmed==x)
 } else {
  fw<-which.min(abs(xmed-x))
  rw<-1+length(x)-which.min(abs(xmed-rev(x)))
  if(fw==rw) {
   wmed<-fw
  } else {
   wmed<-c(fw,rw)
  }
 }
 return(wmed)
}

Jim


On Sun, Jun 5, 2016 at 2:01 AM, Gafar Matanmi Oyeyemi
<gmoyeyemi at gmail.com> wrote: