Skip to content
Prev 673 / 885 Next

subsetting data with the median position

hmmm, maybe those were a little shortsighted and fasttyped...
.. but that should reliably split:

median_split <- function(x){
  n <- floor(length(x) / 2) 
  x <- sort(x)
  list(head(x, n), tail(x, n))
}

median_split(c(2, 4, 8, 9,11,11,12))
## [[1]]
## [1] 2 4 8
## 
## [[2]]
## [1] 11 11 12

median_split(c(2,4,8,9,11,11,11,12,15))
## [[1]]
## [1] 2 4 8 9
## 
## [[2]]
v[1] 11 11 12 15

median_split(0)
## [[1]]
## numeric(0)
## 
## [[2]]
## numeric(0)


best, peter