Scaling a numeric matrix in R with values
Last Updated :
18 Apr, 2024
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.
Similar Reads
Array vs Matrix in R Programming The data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. The two most important data structures in R ar
3 min read
Scale the Columns of a Matrix in R Programming - scale() Function 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 log
2 min read
Create a numeric vector in R A one-dimensional array containing numerical data is called a numeric vector in R Programming Language. Numerical values, such as integers or real numbers, are often stored and manipulated using numerical vectors. Concepts related to the topicNumerical Data Types: Real and integer numbers may be rep
2 min read
Merge two matrices by row names in R In this article, we will examine various methods to merge two matrices by row names in the R programming language. What is a matrix?A matrix is defined as it is a two-dimensional data set which is the collection of rows and columns. A matrix can have the ability to contain or accept data of the same
4 min read
How to Make a Scatter Plot Matrix in R A scatterplot matrix is ââa grid of scatterplots that allows us to see how different pairs of variables are related to each other. We can easily generate a scatterplot matrix using the pairs() function in R programming. In this article, we will walk through the process of creating a scatterplot matr
6 min read
Numeric Types in MATLAB Numeric class in MATLAB includes signed and unsigned integers, single-precision floating-point numbers, and double-precision floating-point numbers. Generally, MATLAB stores all numeric values as double-precision floating-point. But, we can choose to store any number, or, an array of numbers, as int
3 min read
Format Number as Percentage in R In this article, we are going to see how to format numbers as percentages in R programming language. Method 1: Using formattable package The "formattable" package provides methods to create formattable vectors and data frame objects. This package in R can be installed and loaded into the working spa
3 min read
Convert a matrix into a lower triangular matrix using R In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language. What is a matrix?A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are ve
5 min read
Append a row to a matrix using R In this article, we will examine various methods to append a row into a matrix in the R Programming Language. What is a matrix?A matrix is a two-dimensional data set, which is a collection of rows and columns. It contains n rows and m columns. Inside the matrix, rows are arranged horizontally and co
4 min read