How to standardize columns in an R data frame?


This can be done by using scale function.

Example

> data<- data.frame(x = rnorm(10, 50, 1.5), y = runif(10, 2, 5))
> data
      x        y
1  49.57542 2.940931
2  49.51565 2.264866
3  50.70819 2.918803
4  49.09796 2.416676
5  49.90089 2.349696
6  49.03445 3.883145
7  51.29564 4.072614
8  49.11014 3.526852
9  49.41255 3.320530
10 49.42131 3.033730
> standardized_data <- scale(data)
> standardized_data
x y
[1,]  -0.1774447 -0.20927607
[2,]  -0.2579076 -1.28232321
[3,]   1.3476023 -0.24439768
[4,]  -0.8202493 -1.04137095
[5,]   0.2607412 -1.14768085
[6,]  -0.9057468  1.28619932
[7,]   2.1384776  1.58692277
[8,]  -0.8038439  0.72069363
[9,]  -0.3967165  0.39321942
[10,] -0.3849124 -0.06198639
attr(,"scaled:center")
    x       y
49.707220 3.072784
attr(,"scaled:scale")
    x         y
0.7427788 0.6300430

Updated on: 06-Jul-2020

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements