Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
Created February 23, 2017 00:40
Show Gist options
  • Save johnmyleswhite/8651299055752ab9e9724a812df3e7a9 to your computer and use it in GitHub Desktop.
Save johnmyleswhite/8651299055752ab9e9724a812df3e7a9 to your computer and use it in GitHub Desktop.
R's Medians as a Rabbit Hole of Type Promotions and Function Indirection
> median(FALSE)
[1] FALSE
> median(c(TRUE, FALSE))
[1] 0.5
> median(c(TRUE, FALSE, TRUE))
[1] TRUE
> f <- factor(c('a', 'b', 'c'), levels = c('a', 'b', 'c'), ordered = TRUE)
> median(f)
Error in median.default(f) : need numeric data
> median(c('a', 'b'))
[1] NA
Warning message:
In mean.default(sort(x, partial = half + 0L:1L)[half + 0L:1L]) :
argument is not numeric or logical: returning NA
> median(c('a', 'b', 'c'))
[1] "b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment