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
Example 1: Standardize Values Manually
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
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") #  4.7 # attr(,"scaled:scale") #  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
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.
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.