VOOZH about

URL: https://www.geeksforgeeks.org/r-language/scale-the-columns-of-a-matrix-in-r-programming-scale-function/

⇱ Scale the Columns of a Matrix in R Programming - scale() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scale the Columns of a Matrix in R Programming - scale() Function

Last Updated : 17 Apr, 2025

The scale() function in R is used to center and/or scale the columns of a numeric matrix. It helps standardize data by transforming it to have a mean of 0 and standard deviation of 1 .

Syntax

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

Parameters:

  • x: represents numeric matrix .
  • center: represents either logical value or numeric alike vector equal to the number of x .
  • scale: represents either logical value or numeric alike vector equal to the number of x.

Example 1:

In this example, a 2x5 matrix is created and then standardized using the scale() function, which centers each column by subtracting its mean and scales it by dividing by its standard deviation.

Output:

👁 Scale-funtion-on-2x5
Scaling a 2x5 matrix

Example 2:

In this example, a 5x4 matrix is created and then the scale() function is used in two ways: first to center the matrix by subtracting a specified vector of values from each column and second to scale the matrix by dividing each column by a specified vector of values.

Output:

👁 scale-on-5x4
Scaling a 5x4 matrix
Comment
Article Tags:

Explore