Open In App

Scaling a numeric matrix in R with values

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Scaling a numeric matrix in R Programming Language is a common preprocessing step in data analysis and machine learning. It involves transforming the data so that each column has a mean of zero and a standard deviation of one. This process, known as standardization or z-score normalization, helps bring all variables to the same scale, which is essential for certain algorithms and analyses. In this article, we’ll explore how to scale a numeric matrix in R using the scale() function with multiple examples.

Scaling a Simple Numeric Matrix

Let’s start with a simple example of scaling a numeric matrix.

R
# Create a numeric matrix (example data)
numeric_matrix <- matrix(c(10, 20, 30, 40, 50, 60), nrow = 3, ncol = 2)
# Display the original matrix
print("Original Matrix:")
print(numeric_matrix)
# Scale the numeric matrix
scaled_matrix <- scale(numeric_matrix)
# Display the scaled matrix
print("Scaled Matrix:")
print(scaled_matrix)

Output:

[1] "Original Matrix:"
     [,1] [,2]
[1,]   10   40
[2,]   20   50
[3,]   30   60

[1] "Scaled Matrix:"
     [,1] [,2]
[1,]   -1   -1
[2,]    0    0
[3,]    1    1
attr(,"scaled:center")
[1] 20 50
attr(,"scaled:scale")
[1] 10 1

In this example, we create a numeric matrix numeric_matrix with 3 rows and 2 columns. We scale this matrix using the scale() function, which standardizes each column independently. The resulting scaled matrix scaled_matrix has a mean of zero and a standard deviation of one for each column.

Scaling a Numeric Matrix with Custom Mean and Standard Deviation

Often, you’ll work with dataframes instead of matrices. Here’s how you can scale the numeric columns of a dataframe.

R
# Create a numeric matrix
numeric_matrix <- matrix(c(10, 20, 30, 40, 50, 60), nrow = 3, ncol = 2)

# Define custom mean and standard deviation values
custom_mean <- c(5, 10)
custom_sd <- c(2, 3)

# Scale the numeric matrix with custom values
scaled_matrix <- scale(numeric_matrix, center = custom_mean, scale = custom_sd)

# Display the original and scaled matrices
print("Original Matrix:")
print(numeric_matrix)
print("Scaled Matrix with Custom Mean and SD:")
print(scaled_matrix)

Output:

[1] "Original Matrix:"
     [,1] [,2]
[1,]   10   40
[2,]   20   50
[3,]   30   60

[1] "Scaled Matrix with Custom Mean and SD:"
     [,1]     [,2]
[1,]  2.5 10.00000
[2,]  7.5 13.33333
[3,] 12.5 16.66667
attr(,"scaled:center")
[1]  5 10
attr(,"scaled:scale")
[1] 2 3

In this example, we create a dataframe df with two numeric columns A and B. We scale the numeric columns using the scale() function and convert the resulting matrix back into a dataframe using as.data.frame(). The scaled dataframe scaled_df has the same structure as the original dataframe but with scaled values.

Conclusion

Scaling a numeric matrix in R is a fundamental preprocessing step in data analysis and machine learning. In this article, we’ve covered how to scale a numeric matrix using the scale() function with examples. Whether you’re working with matrices or dataframes, scaling helps ensure that your data is on the same scale, facilitating accurate analyses and model training.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads