
What is a Z-score?
A Z-score is a measure calculated for each and every individual from a sample, that takes into account the individual’s personal score, the sample’s mean and the sample’s standard deviation, and thus, attributes each and every individual from a certain sample, to the sample it originated from.
How can we calculate the Z-score for individuals from a given sample?
We can use the mathematical way and the built-in function scale() in R Studio.
The mathematical way:
For the Z-score we need the mean of the sample, and the standard deviation of the sample.
The mean’s calculation is:

The standard deviation calculation is:

And once we have the mean and the standard deviation, this is how mathematically we can calculate the Z-score for each individual:

Let’s see how it’s done in R studio:
For regeneration reasons, we’ll use set.seed() function with the value 1000.
We’ll also use the package Flextable, for a better-looking table:
There’s the code relevant for the sample and the result:
library(dplyr)
library(flextable)
set.seed(1000)
sample <- runif(n = 10, min = 1, max = 100) %>% floor()
sample <- as.data.frame(sample)
sample %>% flextable()
There’s the table produced:

Now there’s the calculation for the mean and standard deviation of the sample:
x_bar <- mean(sample$sample)
sd <- sd(sample$sample)
And there are the values of the mean and standard deviation obtained:
> x_bar
[1] 42.9
> sd
[1] 26.07447
This is how we calculate the Z-score manually:
sample$Z_by_hand <- (sample$sample - x_bar)/sd
And this is the result, with the new column:

Now let’s calculate the z-score using the scale() function:
sample$z_with_scale <- scale(sample$sample)
And this is the result:

It can be seen that the results are completely the same.