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?
quantile(x,0.5,type=c(1,3))
handle non-numericx