Scale the Columns of a Matrix in R Programming - scale() Function Last Updated : 17 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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. R mt <- matrix(1:10, ncol = 5) cat("Matrix:\n") head(mt) # Scale matrix with default arguments cat("\nAfter scaling:\n") scale(mt) Output:Scaling a 2x5 matrixExample 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. R mt <- matrix(1:20, ncol = 4) head(mt) # Scale center by vector of values cat("\nScale center by vector of values:\n") scale(mt, center = c(1, 2, 3, 4), scale = FALSE) # Scale by vector of values cat("\nScale by vector of values:\n") scale(mt, center = FALSE, scale = c(1, 2, 3, 4)) Output:Scaling a 5x4 matrix Comment More infoAdvertise with us Next Article Scale the Columns of a Matrix in R Programming - scale() Function U utkarsh_kumar Follow Improve Article Tags : R Language R Matrix-Function Similar Reads Get or Set Dimensions of a Matrix in R Programming - dim() Function The dim() function in R is used to get or set the dimensions of an object. This function is particularly useful when working with matrices, arrays, and data frames. Below, we will discuss how to use dim() to both retrieve and set dimensions, with examples for better understanding.Syntax: dim(x)Param 2 min read Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function colSums() function in R Language is used to compute the sums of matrix or array columns. Syntax: colSums (x, na.rm = FALSE, dims = 1) Parameters: x: matrix or array dims: this is integer value whose dimensions are regarded as âcolumnsâ to sum over. It is over dimensions 1:dims. Example 1: Python3 # 2 min read Rename Columns of a Data Frame in R Programming - rename() Function The rename() function in R Programming Language is used to rename the column names of a data frame, based on the older names.Syntax: rename(x, names) Parameters:x: Data frame names: Old name and new name 1. Rename a Data Frame using rename function in RWe are using the plyr package to rename the col 2 min read Getting a Matrix of number of columns in R Programming - col() Function col() function in R Language is used to get a matrix which contains the number of columns of the matrix passed to it as argument. Syntax: col(x, as.factor=FALSE) Parameters: x: matrix as.factor: a logical value indicating whether the value should be returned as a factor of column labels (created if 2 min read Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function rownames() function in R Language is used to set the names to rows of a matrix. Syntax: rownames(x) <- value Parameters: x: Matrix value: Vector of names to be set Example: Python3 1== # R program to provide a name # to rows of a Matrix # Creating a 3X3 Matrix A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 1 min read Like