Hello, folks! Hope you all are doing well. Today, we would be focusing on one of the interesting concepts of R programming – **R scale() function**, in detail.

Table of Contents

## What is a scale() function in R?

**Scaling **is an important step in the domain of Data Science and Modelling. R programming provides us with various functions to scale the data. The scale() function is one of them.

The `scale() function`

scales and centers the data values occupied by the columns of a numeric matrix. Yes, you got it right!

**A single function with dual features:**

- By
**centering**values, the`scale() function`

subtracts the values of every column by the corresponding ‘center’ value from the argument only if the value provided is numeric. If a logical value is encountered, the mean of the column gets subtracted from the respective columns of the matrix. - To perform
**scaling**, the scale() function divides the values of every column by the corresponding ‘scale’ value from the argument if the value is numeric. Else the values get divided by the standard deviation or the root mean square values.

Having said this, let us now focus on the implementation!

## Implementing the scale() function in R

Now, let us implement the concept of scaling through the below example! In the below example, we have created an **R matrix** with a number of columns as 3.

Further, we perform the scale() function with ‘**center**‘ and ‘**scale**‘ parameters set to the default values i.e. True.

```
rm(list = ls())
mat <- matrix(1:4, ncol = 3)
print("Original Matrix:")
print(mat)
print("Matrix after scaling:\n")
scale(mat)
```

**Output:**

As seen below, it is witnessed that the matrix has been centralized and scaled using the scale() function easily.

```
> print("Original Matrix:")
[1] "Original Matrix:"
> print(mat)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 4 2
> print("Matrix after scaling:\n")
[1] "Matrix after scaling:\n"
> scale(mat)
[,1] [,2] [,3]
[1,] -0.7071068 -0.7071068 -0.7071068
[2,] 0.7071068 0.7071068 0.7071068
attr(,"scaled:center")
[1] 1.5 3.5 1.5
attr(,"scaled:scale")
[1] 0.7071068 0.7071068 0.7071068
```

### I. Centralizing values with scale() function

If we set `'scale' = FALSE`

, we try to impose that the scaling feature of the scale() function is turned off and that only centralization of the data values will take place.

While centralizing the values, we need to make sure that the value of the parameter ‘center’ is equivalent to the number of columns.

**Example:**

```
rm(list = ls())
mat <- matrix(1:4, ncol = 3)
print("Original Matrix:")
print(mat)
print("Matrix after scaling:\n")
scale(mat, center = c(1,2,3), scale = FALSE)
```

In the above example, we have created a matrix of 3 columns and have passed the tree column indexes to the center parameter of the scale() function as shown.

**Output:**

```
> print("Original Matrix:")
[1] "Original Matrix:"
> print(mat)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 4 2
>
> print("Matrix after scaling:\n")
[1] "Matrix after scaling:\n"
> scale(mat, center = c(1, 2,3), scale = FALSE)
[,1] [,2] [,3]
[1,] 0 1 -2
[2,] 1 2 -1
attr(,"scaled:center")
[1] 1 2 3
```

### II. Scaling with R scale() function

On similar lines, we can scale the values of a matrix by setting `center = FALSE`

in the ** scale() function** as an argument.

Have a look at the below example!

```
rm(list = ls())
mat <- matrix(1:4, ncol = 3)
print("Original Matrix:")
print(mat)
print("Matrix after scaling:\n")
scale(mat, scale = c(1, 2,3), center = FALSE)
```

**Output:**

```
> print("Original Matrix:")
[1] "Original Matrix:"
> print(mat)
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 4 2
>
> print("Matrix after scaling:\n")
[1] "Matrix after scaling:\n"
> scale(mat, scale = c(1, 2,3), center = FALSE)
[,1] [,2] [,3]
[1,] 1 1.5 0.3333333
[2,] 2 2.0 0.6666667
attr(,"scaled:scale")
[1] 1 2 3
```

## Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R, stay tuned and till then, Happy Learning!! 🙂