How to Scale Only Numeric Columns in R, To scale only the numeric columns in a data frame in R, use the dplyr package’s following syntax.

Best Books to learn Tensorflow – Data Science Tutorials

library(dplyr) df %>% mutate(across(where(is.numeric), scale))

How to actually use this function is demonstrated in the example that follows.

Use dplyr to Scale Only Numeric Columns as an example.

Let’s say we have the R data frame shown below, which contains details about numerous basketball players.

## How to Scale Only Numeric Columns in R

Letâ€™s create a data frame

df <- data.frame(Team=c('P1', 'P2', 'P3', 'P4', 'P5'), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â points=c(2, 3, 7, 22, 8), Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â value=c(27, 39, 49, 82, 54))

Now we can view the data frame

df

Team points value 1Â Â P1Â Â Â Â Â 2Â Â Â 27 2Â Â P2Â Â Â Â Â 3Â Â Â 39 3Â Â P3Â Â Â Â Â 7Â Â Â 49 4Â Â P4Â Â Â Â 22Â Â Â 82 5Â Â P5Â Â Â Â Â 8Â Â Â 54

**Technical Remarks**

The following fundamental syntax is used by R’s scale() function.

Best Books to Learn Statistics for Data Science (datasciencetut.com)

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

where:

**x:** Name of the object to scale

**center:** whether to scale after subtracting the mean. As a rule, TRUE.

**scale:** Whether to scale after dividing by the standard deviation. As a general, TRUE.

Scaled values are calculated using the following formula by this function:

xscaled = (xoriginal â€“ xÌ„) / s

where:

**xoriginal:** The original x-value

**xÌ„: **The sample mean

**s:** The sample standard deviation

This process, which only changes each original value into a z-score, is also known as normalizing data.

Let’s say we want to scale the data frame’s numeric columns solely, using R’s scale function.

Methods for Integrating R and Hadoop complete Guide – Data Science Tutorials

To do this, we can use the syntax shown below.

library(dplyr)

scale just the data frame’s numerical columns.

df %>% mutate(across(where(is.numeric), scale))

TeamÂ Â Â Â Â pointsÂ Â Â Â Â value 1Â Â P1 -0.79813157 -1.1284228 2Â Â P2 -0.67342351 -0.5447558 3Â Â P3 -0.17459128 -0.0583667 4Â Â P4Â 1.69602958Â 1.5467175 5Â Â P5 -0.04988322Â 0.1848279

The team column has remained the same, but the values in the three numerical columns (points, assists, and rebounds) have been scaled.