在R中生成数字
问题描述:
在R中,如何生成平均值为X,中位数为Y(至少接近)的N个数字.
In R, how can I generate N numbers that have a mean of X and a median of Y (at least close to).
或更一般地说,是否有一种算法?
Or perhaps more generally, is there an algorithm for this?
答
有无数种解决方案.
近似算法:
- 在中位数以下生成n/2个数字
- 在中位数上方生成n/2个数字
- 添加所需的中位数并检查
- 添加一个权重足以满足您的均值的数字-您可以解决
假设您希望中位数为零且平均值为20的示例:
Example assuming you want a median of zero and a mean of twenty:
R> set.seed(42)
R> lo <- rnorm(10, -10); hi <- rnorm(10, 10)
R> median(c(lo,0,hi))
[1] 0 # this meets our first criterion
R> 22*20 - sum(c(lo,0,hi)) # (n+1)*desiredMean - currentSum
[1] 436.162 # so if we insert this, we the right answer
R> mean(c(lo,0,hi,22*20 - sum(c(lo,0,hi))))
[1] 20 # so we meet criterion two
R>
因为desiredMean times (n+1)
必须等于sum(currentSet) + x
,所以我们解决了x
得到上面的表达式.
because desiredMean times (n+1)
has to be equal to sum(currentSet) + x
so we solve for x
getting the expression above.