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