scale
是你的朋友在这里正常化的意思是= 0,SD = 1,如果SD = 1,VAR = 1。
> mean(scale(1:10))
[1] 0
> sd(scale(1:10))
[1] 1
> var(scale(1:10))
[,1]
[1,] 1
尝试一些示例数据:
set.seed(42)
dat <- data.frame(freq=sample(1:100), scores=rnorm(100, mean=4, sd=2))
dat$bins <- cut(dat$freq, breaks=c(0, 1:10*10), include.lowest=TRUE)
现在使用ave
到scale
的scores
在每个bins
的:
dat$scaled <- with(dat,ave(scores,bins,FUN=scale))
您可以检查结果与aggregate
或类似:
mean
在每个bin中为0(或者非常接近舍入误差)。
> aggregate(scaled ~ bins, data=dat, FUN=function(x) round(mean(x), 2))
bins scaled
1 [0,10] 0
2 (10,20] 0
3 (20,30] 0
4 (30,40] 0
5 (40,50] 0
6 (50,60] 0
7 (60,70] 0
8 (70,80] 0
9 (80,90] 0
10 (90,100] 0
的sd
在每个箱1:
> aggregate(scaled ~ bins, data=dat, FUN=sd)
bins scaled
1 [0,10] 1
2 (10,20] 1
3 (20,30] 1
4 (30,40] 1
5 (40,50] 1
6 (50,60] 1
7 (60,70] 1
8 (70,80] 1
9 (80,90] 1
10 (90,100] 1
这一个班轮完美的作品! – Misconstruction