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

# How to Calculate Z Score in R?

In this article, I will explain how to calculate 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 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

• Step-1: Create the vector for the given data
• Step-2: Calculate the mean of the vector using the mean() function.
• Step-3: Calculate the standard deviation of the vector using the sd() function.
• Step-4: Calculate the z-score using the formula.

z-score = `data- mean(data)) / sd(data)`

• Step-5: The resultant vector will give the required z-score values.

Cool Tip: Read more on how to calculate 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 where we have sample data points from a given data set to find a z score.

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

First, create a vector for given data points and assign it to the data variable.

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

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

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.

Use below R code to calculate 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)
 15.42857
>

> sd(data)
 3.309438

> zscore
  1.0791646 -1.3381641 -1.0359980 -0.4316658
 -0.1294998  0.7769985  1.0791646

``````

### Interpretation of z-scores

• The z-score for data value 19 is 1.0791646. It means it is 1.0791646 standard deviation above the mean.
• The z-score for data value 11 is -1.3381641. It means it is -1.3381641 standard deviation below the mean.
• The z-score for data value 12 is -1.0359980. It means it is -1.0359980 standard deviation below the mean.
• The z-score for data value 14 is -0.4316658. It means it is -0.4316658 standard deviation below the mean.

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 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.