Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic method of finding the median of an ordinal

I need to find the median of an ordinal (i.e. ordered factor) in R.

I couldn't find a method in the standard library to do this, so I came up with the following clunky solution:

ordinal.median <- function(x){
        lbls <- levels(x)
        new.vars <- c(NA, 1:length(lbls))
        new.vars[1] <- median(as.numeric(x))
        return(factor(new.vars, labels=lbls, ordered=T)[1])
}

What would be the idiomatic solution to this in R?

like image 592
fmark Avatar asked Dec 06 '25 10:12

fmark


1 Answers

You can simplify it a bit (and note that ordered is the class for ordinal factors, so you can call this with just median(o) where o is your variable):

median.ordered <- function(x)
{
    levs <- levels(x)
    m <- median(as.integer(x))
    if(floor(m) != m)
    {
      warning("Median is between two values; using the first one")
      m <- floor(m)
    }
    ordered(m, labels = levs, levels = seq_along(levs))
}

Usage:

median(ordered(c("A", "B", "C"))) 
median(ordered(c("A", "B", "A", "B")))
like image 125
Hong Ooi Avatar answered Dec 08 '25 00:12

Hong Ooi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!