首页 > 解决方案 > 仅标准化数据集中(R)中的一些变量的正确语法是什么?

问题描述

起初我试过:

Bank_sc <- preProcess(x = Bank,
                      method = c("center", "scale"), 
                      select=c(Age, Experience, Income, Family, CCAvg, Education, Mortgage))

我在这里省略了一个变量,但它仍然是标准化的。我找不到任何关于正确语法的文章,所以请帮忙。

标签: rstandardization

解决方案


您可以使用 dplyr 包

data <- data.frame(x= sample(1:100, 30), y = sample(1:100, 30), z= sample(1:100, 30))

head(data)
   x  y  z
1 26 60 16
2 38 52 51
3 12 25 13
4 32 78 54
5  6 71 59
6 10 83  3

library(dplyr)

data <- data %>% mutate_at(vars(x, y), scale)

head(data)
           x          y  z
1 -0.6630489  0.1550407 16
2 -0.2522096 -0.1088584 51
3 -1.1423613 -0.9995179 13
4 -0.4576293  0.7488137 54
5 -1.3477809  0.5179020 59
6 -1.2108345  0.9137507  3

推荐阅读