Welcome to the DataHall youtube channel in this video we are going to talk about
how do you standardize a variable in R. Now standardization is a method that we
use to put different variables on a same scale so if you want to compare different variables that have different
scale for that what we do is we we standardize these variables. Now there are different ways of
standardization one is z-score then we have mean centering and then we have a different method that is called normalization. Now these are
what we are going to look into in how do we perform these in R. So let's
start with z-score. Let's create this data now what z-score does is we deduct the the mean value of a
variable from each of its value and divide each value by its standard deviation so that way we have
a variable that would have a mean of zero and standard deviation of one. Now for that we have this function which is called
scale and let's say we have this data set let's create this data set we have this data
we have this vector data called data and we want to scale it and save it into
this standard into this object called standard. What we do is we use this scale and if I can show you the mean of this
value variable or this vector it is zero and if I can show you the standard deviation or variance
it is equal to one. We can do the same process on a data frame so let's say we have this empty cars data frame
that contain different variables related to different variables related to car their mileage their their gear ratio etc etc
and let's say we want to standardize this mpg variable which stands for
for mileage again we use the scale variable and we take into account the column that we want to standardize
and what is the new column that would be created if I execute this
we we get this column in the in our data frame. Okay so let's move to the second part which is called
normalization. Now normalization is somewhat different from from calculating z-score. Z-score would have a standard deviation of one
and a mean of zero but in normalization we would scale a variable to a
fixed range so typically that range is from zero to one so for that we would have to install the stydevers package
because you do some data wrangling with the stydevers package now I have already installed it if you haven't you would use this command first
and then load this library using library and name of the the package that we want to load. Now
let's take the empty cars data and create mutate this new column that is called normal mpg and that would be equal to
each value of mpg minus the minimum value of this mpg column now this whole divided by the range which is the maximum of mpg minus
the minimum mpg if I do that we get we get a column that is that is called a normal mpg and that
is now normalized we can create box plot of our mpg column and and we can also create a box plot
of our standardized or normalized variable now that would give us if there are outliers these outliers would
also be taken care of this is what one of the benefits of standardization
or normalization. The last technique is called mean centering and in mean centering what we do is
we only deduct the mean of the variable from each value and do not divide it by standard deviation so that way the standard
deviation remains the same as of the original variable but its mean
is now zero so for that we take this displacement variable minus the mean of
this variable and I am setting this parameter equal to true so that it removes the missing value
so I have pressed ctrl enter and if I can show you the mean of
and standard deviation of the original variable and also the newly created variable now this is the mean of our original
variable and this is the mean of the normalized variable its value is
equal to zero mean value zero the standard deviation is one two three and it remains the same when we do mean centering but when we do
standardization the mean value would be zero and the standard deviation would be equal to one so I hope this video was useful do
subscribe to this channel and do hit the bell icon thanks for
watching this video