Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • Statistical test assumptions and requirements
    Statistical test assumptions and requirements Statistics
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • Crosstab calculation in R
    Crosstab calculation in R R
  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
How to Scale Only Numeric Columns in R

How to Scale Only Numeric Columns in R

Posted on August 4August 4 By Jim No Comments on How to Scale Only Numeric Columns in R
Tweet
Share
Share
Pin

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.

Check your inbox or spam folder to confirm your subscription.

How to Standardize Data in R? – Data Science Tutorials

Tweet
Share
Share
Pin
R Tags:dplyr, mutate

Post navigation

Previous Post: Best Books to Learn Statistics for Data Science
Next Post: Best Books About Data Analytics

Related Posts

  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • How to Get a Job as a Data Engineer
    How to Get a Job as a Data Engineer? R
  • Filter Using Multiple Conditions in R
    Filter Using Multiple Conditions in R R
  • Survival Plot in R
    How to Perform a Log Rank Test in R R
  • The Multinomial Distribution in R
    The Multinomial Distribution in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • gganatogram Plot in R
    How to create Anatogram plot in R R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • pheatmap function in R
    The pheatmap function in R R
  • sorting in r
    Sorting in r: sort, order & rank R Functions R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R
  • The Multinomial Distribution in R
    The Multinomial Distribution in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme