# generate data and weights
set.seed(1)
x <- rnorm(10)
w <- rpois(10, lambda = 10)
# weighted mean
wtd.mean(x, w)
sum(x * w) / sum(w)
# trimmed mean
q <- quantile(x, probs = c(0.1, 0.9), type = 4)
i <- which(x < q[1] | x > q[2])
mean(x[-i])
wtd.mean(x, trim = 0.1)
# weighted and trimmed mean
q <- wtd.quantile(x, w, probs = c(0.1, 0.9))
i <- which(x < q[1] | x > q[2])
wtd.mean(x[-i], w[-i])
wtd.mean(x, w, trim = 0.1)
Run the code above in your browser using DataLab