# NOT RUN {
Median(1:4) # = 2.5 [even number]
Median(c(1:3, 100, 1000)) # = 3 [odd, robust]
# Approximation for classified data
breaks <- seq(10,70, 10)
Median(
Freq(cut(d.pizza$temperature, breaks=breaks)),
breaks=breaks)
# compared to
Median(d.pizza$temperature)
# starting from a classified table
# from to income
# 0 4000 20
# 4000 6000 42
# 6000 8000 31
# 8000 10000 12
# Freq(as.table(c(20,42,31,12)))
# level freq perc cumfreq cumperc
# 1 A 20 19.0% 20 19.0%
# 2 B 42 40.0% 62 59.0%
# 3 C 31 29.5% 93 88.6%
# 4 D 12 11.4% 105 100.0%
Median(Freq(as.table(c(20,42,31,12))), breaks=c(0,4000,6000,8000,10000))
# use weights
x <- sample(20, 30, replace = TRUE)
z <- as.numeric(names(w <- table(x)))
(m1 <- Median(z, weights=w))
(m2 <- Median(x))
stopifnot(identical(m1, m2))
# }
Run the code above in your browser using DataLab