Home » Z Score Calculation » How to Calculate Z-Score in R?

# How to Calculate Z-Score in R?

R doesn’t have an inbuilt function to calculate the z-score in R. To get the z-score in R, calculate the mean and standard deviation using the mean() and sd() functions in R and use mean and std. deviation values in the Z-score formula to find the z-score in R.

In this article, we will how to calculate the z score in R using the z score formula. We will calculate the mean and standard deviation for the vector using the r function and later calculate the z score.

## What is a Z-Score?

Z-score is the distance of the raw score value from the mean in terms of standard deviation. Raw scores above the mean have a positive Z-score value, while a raw score below the mean has a negative z-score value.

## Steps to Calculate Z-score in R

1. Create the vector for the given data
2. Calculate the mean of the vector using the mean() function.
3. Calculate the standard deviation of the vector using the sd() function.
4. Calculate the z-score using the formula. z-score = data- mean(data)) / sd(data)
5. The resultant vector will give the required z-score values.

Cool Tip: Read more on how to calculate the z score on TI-84 plus!

Let’s understand how to find a z-score using R for a given vector using the step-by-step guide.

## Calculate Z-score in R for the given vector

Let’s consider an example to get a z-score in R using the given vector data set.

### Prepare vector in r to find the mean and standard deviation

Create a vector for given data points and assign it to the data variable.

``````# create the vector for given data
data <- c(19, 11, 12, 14, 15, 18, 19)``````

Use the mean function to calculate the mean for a given vector.

``````# Calculate mean
mean(data)``````

Use the standard deviation (sd) function to calculate the standard deviation for a given vector.

``````# Calculate standard deviation
sd(data)``````

To calculate the z-score in r put the calculated mean and standard deviation in the z score formula, it will find the z-score and assign it to the zscore variable.

``````# calculate z-score
zscore <- (data - mean(data)) / sd(data)``````

Use below R code to calculate the z-score.

### Z-Score in R code

``````# create the vector for given data
data <- c(19, 11, 12, 14, 15, 18, 19)

# Calculate mean
mean(data)

# Calculate standard deviation
sd(data)

# calculate z-score
zscore <- (data - mean(data)) / sd(data)

zscore``````
``````

#Results

mean(data)
[1] 15.42857
>

> sd(data)
[1] 3.309438

> zscore
[1]  1.0791646 -1.3381641 -1.0359980 -0.4316658
[5] -0.1294998  0.7769985  1.0791646

``````

### Interpretation of z-scores in R

1. The z-score for data value 19 is 1.0791646. This means the raw score of 19 is 1.0791646 standard deviation above the mean of 15.42857.
2. The z-score for data value 11 is -1.3381641. This means the raw score of 11 is -1.3381641 standard deviation below the mean of 15.42857.
3. The z-score for data value 12 is -1.0359980. This means the raw score of 12 is -1.0359980 standard deviation below the mean of 15.42857.
4. The z-score for data value 14 is -0.4316658. This means the raw score of 14 is -0.4316658 standard deviation below the mean of 15.42857.

Similarly, we can interpret the other z-scores.

Cool Tip: Read more on how to Calculate Z Score in Excel!

## Plot the Calculated Z-score for the given vector in R

``````

# create the vector for given data
data <- c(19, 11, 12, 14, 15, 18, 19)

# Calculate mean
mean(data)

# Calculate standard deviation
sd(data)

# calculate z-score
zscore <- (data - mean(data)) / sd(data)

zscore

# plot z-score
plot(zscore, type="o", col="green")

``````

The resultant plot for the calculated z score for the given data set is shown below.

## Conclusion

I hope the above article helps you to calculate the z score using r. We have learned how to prepare a vector for a given dataset and find the mean and standard deviation.

Using the z-score formula, we can find the z-score in r and plot the z-score on a chart.