Skip to content

Instantly share code, notes, and snippets.

@danlewer
Last active January 3, 2023 12:18
Show Gist options
  • Save danlewer/44ad7f79d7b090910b9de888856f7ddf to your computer and use it in GitHub Desktop.
Save danlewer/44ad7f79d7b090910b9de888856f7ddf to your computer and use it in GitHub Desktop.
which.median <- function (x, ties.method = c('first', 'last', 'random', 'all'), ...) {
med <- median(x, ...)
dif <- abs(med - x)
ind <- which(dif == min(dif))
if (length(ind) > 1 & ties.method[1] != 'all') {
ind <- if (ties.method[1] == 'first') ind[1] else {if (ties.method[1] == 'last') tail(ind, 1) else sample(ind, 1)}
}
return (ind)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment