1. Help Center
  2. Machine Learning

How to standardize variables?

Method 1: Using Scale function.
 
R has a built-in function called scale() for the purpose of standardization.
 
Syntax: scale(x,center=True,scale=True)
 
Here, “x” represents the data column/dataset on which you want to apply standardization. “center” parameter takes boolean values, it will subtract the mean from the observation value when it is set to True. “scale” parameter takes boolean values, it will divide the resulting difference by standard deviation when it is set to True.
 
Approach:
 
Create dataset
Apply scale function on the data column
Convert the vector result to the data frame
Display result
Program:
 
# Creating Dataset
X <- c('A','B','C','D','E','F')
Y <- c(15,16,20,19,19,17)
Z <- c(5.0,4.0,5.0,2.0,1.0,3.0)
 
dataframe <- data.frame(Name = X, Age = Y, CGPA = Z )
 
# applying scale function
dataframe[2 : 3] <- as.data.frame(scale(dataframe[2 : 3]))
 
# displaying result
dataframe
 
Method 2: Using base R
 
Approach:
 
Create Dataset.
Create a function for standardization.
Syntax: standardize = function(x){ z <- (x – mean(x)) / sd(x) return( z)}
 
Apply this function to the data columns.
Convert the vector result to the data frame
Display result
Program:
 
# Creating Dataset
X <- c('A', 'B', 'C', 'D', 'E', 'F')
Y <- c(15, 16, 20, 19, 19, 17)
Z <- c(5.0, 4.0, 5.0, 2.0, 1.0, 3.0)
 
dataframe <- data.frame(Name = X, Age = Y, CGPA = Z )
 
# creating Standardization function
standardize = function(x){
z <- (x - mean(x)) / sd(x)
return( z)
}
 
# apply your function to the dataset
dataframe[2:3] <-
apply(dataframe[2:3], 2, standardize)
 
#displaying result
dataframe