这会单独缩放每一列吗? R

Does this scale each column individually? R

如果我想标准化第 2 列和第 3 列(每列分别标准化),这行得通吗?

df[c(2:3)] <- scale(df[c(2:3)])

根据?scale

The value of scale determines how column scaling is performed (after centering). If scale is a numeric-alike vector with length equal to the number of columns of x, then each column of x is divided by the corresponding value from scale. If scale is TRUE then scaling is done by dividing the (centered) columns of x by their standard deviations if center is TRUE, and the root mean square otherwise. If scale is FALSE, no scaling is done.


作为测试,我们可以对每一列和整个列单独执行此操作

data(mtcars)
out1 <- sapply(mtcars, scale)
out2 <- scale(mtcars)
all.equal(out1, out2, check.attributes = FALSE)
#[1] TRUE

它为每一列分别scale