[R] which function

Jim Lemon drjimlemon at gmail.com
Sun Jun 5 01:57:09 CEST 2016


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:
> 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.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list