Introduction
For a normally distributed variable, a z score assigns a number to each data point based on its distance, in standard deviations, from the mean. For example, if the mean of variable iq is 100, with sd 15, then an iq of 100 has a z score of 0, an iq of 85 has a z score of -1, and an iq of 115 has a z score of 1. In this blog, we’ll show you how to use R code to create z scores for each value in a normal distribution.
Generate Normally Distributed Data
First, let’s generate a normally distributed variable, iq, with 10,000 observations, a mean of 100, and a standard deviation of 15.
iq2 <- rnorm(10000, mean=100, sd=15)
iq <- round(iq2)
mean(iq)
sd(iq)
You can confirm that R has generated an IQ variable with a mean very close to 100 and a standard deviation very close to 15. You can also generate a histogram to visually confirm the normality of IQ distribution:
hist(iq)
Create z Scores
Now try the following code, which will take every value of IQ, subtract the mean from it, and divide it by the standard deviation, leading to the generation of z scores.
iqmean <- mean(iq)
iqsd <- sd(iq)
z <- (iq-iqmean)/iqsd
iqdata <- data.frame(iq, z)
head(iqdata, n=20)
Confirm that each IQ score now has a z score associated with it:
You used iq2, iqmean, and iqsd to help you get to the z scores, so you needn’t include these variables in your data frame.
BridgeText can help you with all of your statistical analysis needs.