calculate the median of intervals of a vector in R

Viewed 60

I have the following challenge: Let X be a V.A. discrete evenly distributed in the interval [1, 10]. Be Y another V.A. each sample of Y is given by the median value of one set of 10 X samples. Generate 10^6 Y samples.

I'm using R, with the following code:

x <- runif(10000000,1,10)
sample <- cut(x,breaks=10)
y = median(sample)
1 Answers

You could do:

 mosaic::median(~x|sample)

or in base R:

 tapply(x, sample, median)
Related