Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R scale() Function - Data Standardization

R scale() Function - Data Standardization

R Language Examples R Language Examples

The R scale() function is used to standardize data (z-score standardization).

Standardized data has a mean of 0 and a standard deviation of 1, often used to eliminate the effects of different scales.

The syntax of the scale() function is as follows:

scale(x, center = TRUE, scale = TRUE)

Parameter Description:

Example

Raw data: variables with different scales

height

<-

c

(

160

,

170

,

165

,

175

,

180

)

   

Range 160-180

income

<-

c

(

50000

,

60000

,

55000

,

80000

,

90000

)

 

Range 50000-90000

age

<-

c

(

25

,

30

,

28

,

35

,

40

)

           

Range 25-40

Combine into a matrix

data_matrix

<-

cbind

(

height, income, age

)

print

(

"Original data:"

)

print

(

data_matrix

)

Standardization

scaled_data

<-

scale

(

data_matrix

)

print

(

"After standardization (mean 0, std 1):"

)

print

(

round

(

scaled_data,

3

)

)

Verify the mean and standard deviation

print

(

"Column means after standardization (should be near 0):"

)

print

(

round

(

colMeans

(

scaled_data

)

,

3

)

)

print

(

"Column standard deviations after standardization (should equal 1):"

)

print

(

round

(

apply

(

scaled_data,

2

,

sd

)

,

3

)

)

The output of the code above is:

[1] "原始数据:"

     height income age

[1,]    160  50000  25

[2,]    170  60000  30

[3,]    165  55000  28

[4,]    175  80000  35

[5,]    180  90000  40

[1] "标准化后(均值为0,标准差为1):"

       height income    age

[1,] -1.2649 -1.2647 -1.2649

[2,]  0.0000 -0.4739  0.0000

[3,] -0.6325 -0.8699 -0.6325

[4,]  0.6325  0.7109  0.6325

[5,]  1.2649  1.8976  1.2649

[1] "标准化后各列均值(应接近0):"

 height  income     age

      0       0       0

[1] "标准化后各列标准差(应等于1):"

height income    age

     1      1      1

R Language Examples R Language Examples