Skip to content

Data Science Tutorials

For Data Science Learners

  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • Apply central limit throem in r
    Apply Central Limit Theorem in R R
  • Determine the significance of a mediation effect in R
    Determine the significance of a mediation effect in R R
  • How to Create a Frequency Table by Group in R
    How to Create a Frequency Table by Group in R? R
  • Box Cox transformation in R
    Box Cox transformation in R R
  • Top 10 Data Visualisation Tools
    Top 10 Data Visualisation Tools Every Data Science Enthusiast Must Know Course
  • How to Change X-Axis Labels of Barplot In R
    How to Change X-Axis Labels of Barplot In R R

Calculating Z-Scores in R: A Step-by-Step Guide

Posted on June 29June 29 By Admin No Comments on Calculating Z-Scores in R: A Step-by-Step Guide

Calculating Z-Scores in R: A Step-by-Step Guide, Z-scores measure how many standard deviations an individual data value is from the mean.

Calculating Z-Scores in R: A Step-by-Step Guide

In this article, we will explore how to calculate Z-scores in R using various methods. We will cover calculating Z-scores for a single vector, a single column in a data frame, and every column in a data frame.

To calculate Z-scores, we use the formula: Z = (X – μ) / σ, where X is a single raw data value, μ is the population mean, and σ is the population standard deviation.

How to draw heatmap in r: Quick and Easy way » Data Science Tutorials

We will start by calculating Z-scores for a single vector using the following code:

data <- c(6, 7, 7, 12, 13, 13, 15, 16, 19, 22)
z_scores <- (data-mean(data))/sd(data)
z_scores

This will give us the Z-score for each data value in the vector. For example, the first raw data value of “6” is 1.323 standard deviations below the mean.

Next, we will calculate Z-scores for a single column in a data frame using the following code:

df <- data.frame(assists = c(4, 4, 6, 7, 9, 13),
points = c(24, 29, 13, 15, 19, 22),
rebounds = c(5, 5, 7, 8, 14, 15))

z_scores <- (df$points-mean(df$points))/sd(df$points)
z_scores
[1] 0.6191904 1.4635409 -1.2383807 -0.9006405 -0.2251601 0.2814502

This will give us the Z-score for each data value in the “points” column. For example, the first raw data value of “24” is 0.619 standard deviations above the mean.

Finally, we will calculate Z-scores for every column in a dataframe using the sapply() function:

df <- data.frame(assists = c(4, 4, 6, 7, 9, 13),
points = c(24, 29, 13, 15, 19, 22),
rebounds = c(5, 5, 7, 8, 14, 15))

sapply(df, function(df) (df-mean(df))/sd(df))

assists points rebounds
[1,] -0.92315712 0.6191904 -0.9035079
[2,] -0.92315712 1.4635409 -0.9035079
[3,] -0.34011052 -1.2383807 -0.4517540
[4,] -0.04858722 -0.9006405 -0.2258770
[5,] 0.53445939 -0.2251601 1.1293849
[6,] 1.70055260 0.2814502 1.3552619

This will give us the Z-score for every raw data value in every data frame column. For example, the first value of “4” in the first column is 0.923 standard deviations below the mean value of its column.

Conclusion

Calculating Z-scores in R is a straightforward process that can be done using various methods.

By understanding how to calculate Z-scores for a single vector and a single column in a dataframe as well as every column in a dataframe, you can better analyze and understand your data.

  • Major Components of Time Series Analysis
  • Sample Size Calculation and Power Clinical Trials
  • Biases in Statistics Common Pitfalls
  • Area Under Curve in R (AUC)
  • Filtering Data in R 10 Tips -tidyverse package
  • How to Perform Tukey HSD Test in R
  • Statistical Hypothesis Testing-A Step by Step Guide
  • How to Create Frequency Tables in R
  • PCA for Categorical Variables in R
  • sweep function in R
R

Post navigation

Previous Post: Descriptive Statistics in R
Next Post: Calculating Autocorrelation in R

Related Posts

  • How to plot categorical data in R
    Plot categorical data in R R
  • Adding Subtitles in ggplot2 R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • Wrap a character string in R R
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • How Do Machine Learning Chatbots Work
    How Do Machine Learning Chatbots Work Machine Learning
  • Quantiles by Group calculation in R
    Quantiles by Group calculation in R with examples R
  • Data Science Strategies for Improving Customer Experience in R
    Data Science Strategies for Improving Customer Experience in R R
  • How to Load the Analysis ToolPak in Excel
    How to Load the Analysis ToolPak in Excel Excel
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Best Git Books R
  • Mastering the table() Function in R R
  • Comparison between Statistics and Luck
    Lottery Prediction-Comparison between Statistics and Luck Machine Learning

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme