Reputation: 409
If I wanted to standardize columns 2 and 3 (each column standardized separately), would this work?
df[c(2:3)] <- scale(df[c(2:3)])
Upvotes: 3
Views: 1136
Reputation: 887741
According to ?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.
As a test, we can do this individually on each column and as a whole
data(mtcars)
out1 <- sapply(mtcars, scale)
out2 <- scale(mtcars)
all.equal(out1, out2, check.attributes = FALSE)
#[1] TRUE
It does scale
separately for each column
Upvotes: 5