如果我在数据帧中缩放和居中数字列(每列中的每个值除以相应的列SD,并从每个值中减去每个相应列的平均值) - 我如何然后反向转换为原始值。
在下面的简单示例中,我看到在应用' scale'之后,每列的平均值和SD都存储在对象d4中。以居中为中心。
d1 <- as.data.frame(seq(1,20,1))
d2 <- as.data.frame(seq(0.11,0.3,0.01))
d3 <- cbind(d1,d2)
names(d3) <- c("A","B")
d4 <- scale(d3,center=TRUE)
d4
A B
[1,] -1.60579308 -1.60579308
[2,] -1.43676223 -1.43676223
[3,] -1.26773138 -1.26773138
[4,] -1.09870053 -1.09870053
[5,] -0.92966968 -0.92966968
[6,] -0.76063883 -0.76063883
[7,] -0.59160798 -0.59160798
[8,] -0.42257713 -0.42257713
[9,] -0.25354628 -0.25354628
[10,] -0.08451543 -0.08451543
[11,] 0.08451543 0.08451543
[12,] 0.25354628 0.25354628
[13,] 0.42257713 0.42257713
[14,] 0.59160798 0.59160798
[15,] 0.76063883 0.76063883
[16,] 0.92966968 0.92966968
[17,] 1.09870053 1.09870053
[18,] 1.26773138 1.26773138
[19,] 1.43676223 1.43676223
[20,] 1.60579308 1.60579308
attr(,"scaled:center")
A B
10.500 0.205
attr(,"scaled:scale")
A B
5.9160798 0.0591608
我现在如何使用存储的均值和SD值从d4计算原始值的数据帧
答案 0 :(得分:1)
我们可以做到
r1 <- d4 * attr(d4, 'scaled:scale')[col(d4)] + attr(d4, 'scaled:center')[col(d4)]
all.equal(as.data.frame(r1), d3)
#[1] TRUE