Message-ID: <1450817849.8439.0@smtp.uni-konstanz.de>
Date: 2015-12-22T20:57:29Z
From: Peter Meißner
Subject: subsetting data with the median position
In-Reply-To: <656066ED-1614-43FB-A883-E47851F977B6@calvin.edu>
hmmm, maybe those were a little shortsighted and fasttyped...
>
> dat1 <- c(2,4,8,9,11,11,11,12,15)
>
> dat1[dat1 > median(dat1)]
> [1] 12 15
> dat1[dat1 < median(dat1)]
> [1] 2 4 8 9
>
.. 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
[[alternative HTML version deleted]]