Compute z-score in R (2 Examples)
This article shows how to calculate z-scores (also called standard scores, z-values, normal scores, and standardized variables) in the R programming language.
The content of the page is structured as follows:
If you want to learn more about these topics, keep reading.
Introducing Example Data
As a first step, we’ll need to construct some data that we can use in the exemplifying syntax later on.
x <- c(7, 6, 1, 4, 3, 5, 3, 7, 6, 5) # Create example data x # Print example date # 7 6 1 4 3 5 3 7 6 5 |
x <- c(7, 6, 1, 4, 3, 5, 3, 7, 6, 5) # Create example data x # Print example date # 7 6 1 4 3 5 3 7 6 5
The previous output of the RStudio console reveals that our example data is a vector consisting of several numeric values. The values of our example data are not standardized / normalized yet.
Example 1: Standardize Values Manually
Example 1 explains how to standardize the values of a vector or data frame column manually by using the mean and sd functions in R.
Have a look at the following R code:
x_stand1 <- (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE) # Standardize manually x_stand1 # Print standardized values # 1.1816039 0.6678631 -1.9008410 -0.3596186 -0.8733594 0.1541222 -0.8733594 1.1816039 0.6678631 0.1541222 |
x_stand1 <- (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE) # Standardize manually x_stand1 # Print standardized values # 1.1816039 0.6678631 -1.9008410 -0.3596186 -0.8733594 0.1541222 -0.8733594 1.1816039 0.6678631 0.1541222
The previous output of the RStudio console shows the standardized values that correspond to our input vector.
Note that we have specified the na.rm argument to be equal to TRUE. In case your data would contain missing values, those values would be removed for the computation of z-scores.
Example 2: Standardize Values Using scale() Function
The previous example shows how to calculate z-scores manually based on its formula. However, the R programming language provides a function called scale, which makes the computation of z-scores easier and more efficient.
We can use the scale function as shown below:
x_stand2a <- scale(x) # Standardize using scale() x_stand2a # Print standardized values # [,1] # [1,] 1.1816039 # [2,] 0.6678631 # [3,] -1.9008410 # [4,] -0.3596186 # [5,] -0.8733594 # [6,] 0.1541222 # [7,] -0.8733594 # [8,] 1.1816039 # [9,] 0.6678631 # [10,] 0.1541222 # attr(,"scaled:center") # [1] 4.7 # attr(,"scaled:scale") # [1] 1.946507 |
x_stand2a <- scale(x) # Standardize using scale() x_stand2a # Print standardized values # [,1] # [1,] 1.1816039 # [2,] 0.6678631 # [3,] -1.9008410 # [4,] -0.3596186 # [5,] -0.8733594 # [6,] 0.1541222 # [7,] -0.8733594 # [8,] 1.1816039 # [9,] 0.6678631 # [10,] 0.1541222 # attr(,"scaled:center") # [1] 4.7 # attr(,"scaled:scale") # [1] 1.946507
As you can see, the scale function returns a matrix instead of a vector. In case you prefer to have a standardized vector, you can modify the output of the scale function as shown below:
x_stand2b <- as.numeric(x_stand2a) # Convert matrix to vector x_stand2b # Print standardized values # 1.1816039 0.6678631 -1.9008410 -0.3596186 -0.8733594 0.1541222 -0.8733594 1.1816039 0.6678631 0.1541222 |
x_stand2b <- as.numeric(x_stand2a) # Convert matrix to vector x_stand2b # Print standardized values # 1.1816039 0.6678631 -1.9008410 -0.3596186 -0.8733594 0.1541222 -0.8733594 1.1816039 0.6678631 0.1541222
The previous output is exactly the same as in Example 1.
Video, Further Resources & Summary
Do you want to learn more about standardization in R? Then I can recommend to have a look at the following video which I have published on my YouTube channel. In the video instruction, I explain the R programming syntax of this tutorial in RStudio.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to have a look at the related tutorials on this website:
To summarize: You learned in this article how to standardize vectors and data frame columns in the R programming language. If you have additional questions, don’t hesitate to let me know in the comments below.
Statistics Globe Newsletter
8 Comments. Leave new
Joachim,
I wish you would put all this wonderful, useful information about R into a book. Very helpful information and much appreciated.
Regards,
Henri Tuthill
Hi Henri,
Thank you very much for such an awesome feedback!
Indeed, I’m planning to release a book or maybe a video series in the future. Unfortunately, I never find the time to do it.
However, this is definitely something that will come sooner or later!
Regards,
Joachim
Hi Joachin,
Your tutorials on R were very useful for me to complete my course on Data Science in Brazil.
Despite not being versed in the language, his objective tutorials and google translator helped me a lot! :-))
Thank you!
Merry Christmas and Prosperous New Year!
Hey Eduardo,
Thank you very much for these kind words. I’m very happy to get such a positive feedback from you! 🙂
Merry Christmas and a happy new year for you as well!
Joachim
This is an amazing tutorial. I have a question if possible it could be answered. Let’s say you have a dataset with multiple columns and you wanted to calculate the z-score for a subset of a certain variable, what would you do then?
For example: calculate the z-score for the female grades
Gender. Grade
Male. 82
Female. 100
Male. 95
Female. 75
Male. 77
Male. 88
Would you use the population mean and standard deviation but use only the female data point when subtracting or do you use only the sample mean and deviation. Thank you for everything.
Hi Moe,
Thanks a lot for the nice comment!
This depends a bit on what you want to evaluate. However, in case you want to analyze the females separately I would only use the mean and standard deviation of the subset of females.
I hope that helps!
Joachim
Thank you so much
Hi Bushra,
Thanks for the feedback! Hope the tutorial was helpful!
Regards,
Matthias