Skip to content

Data Science Tutorials

For Data Science Learners

  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • How to Check if a Directory Exists in R
    How to Check if a Directory Exists in R R
  •  Identify positions in R R
  • How to Specify Histogram Breaks in R R
  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • Compare numeric vectors in R R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R

Descriptive statistics in R

Posted on August 11August 10 By Admin 2 Comments on Descriptive statistics in R

Descriptive statistics in R, it is often necessary to create a table that contains descriptive statistics for variables in a data frame.

One of the best ways to do this is by using the stat.desc() function from the pastecs package in R.

This function can be used to perform a variety of statistical analyses, including calculating descriptive statistics for variables in a data frame.

The Syntax of the stat.desc() Function

The syntax for the stat.desc() function is as follows:

stat.desc(x, basic=TRUE, desc=TRUE, norm=FALSE, p=0.95)

Where:

  • x: The name of the data frame.
  • basic: A boolean value indicating whether to return basic statistics or not.
  • desc: A boolean value indicating whether to return more advanced statistics or not.
  • norm: A boolean value indicating whether to return normal distribution statistics or not.
  • p: The p-value to use when calculating confidence interval values.

Example: Using the stat.desc() Function in R

Suppose that we have a data frame in R that contains information about various basketball players, including their team name, total points scored, and total assists.

We can use the stat.desc() function to calculate descriptive statistics for each of the columns in the data frame.

Here is an example of how to use the stat.desc() function:

# Load the pastecs package
library(pastecs)

# Create a data frame
df <- data.frame(team=c('P1', 'P1', 'P1', 'P2', 'P2', 'P2', 'P3', 'P3'),
points=c(220, 309, 124, 218, 125, 110, 128, 123),
assists=c(13, 18, 18, 12, 15, 12, 18, 12))

# View the data frame
df

# Calculate descriptive statistics for each column in the data frame
stat_desc(df)

When we run this code, we get a table of descriptive statistics for each of the columns in the data frame.

Convert a continuous variable to a categorical in R » Data Science Tutorials

This table includes information such as the number of values, null values, and NA values for each column, as well as the minimum and maximum values for each column.

Interpreting the Output

The output of the stat.desc() function is a table that includes a variety of statistical measures. Here’s how to interpret each of these measures:

  • nbr.val: The number of values in the column.
  • nbr.null: The number of null values in the column.
  • nbr.na: The number of NA values in the column.
  • min: The minimum value in the column.
  • max: The maximum value in the column.
  • range: The range (max – min) of values in the column.
  • sum: The sum of values in the column.
  • median: The median value in the column.
  • mean: The mean value in the column.
  • SE.mean: The standard error of the mean value.
  • CI.mean .95: The 95% confidence interval for the mean value.
  • var: The variance of values in the column.
  • std.dev: The standard deviation of values in the column.
  • coef.var: The coefficient of variation of values in the column.

Using the stat.desc() Function with Multiple Columns

If you want to calculate descriptive statistics for multiple columns in a data frame, you can use the following syntax:

# Calculate descriptive statistics for points and assists columns
stat_desc(df[c('points', 'assists')])

This will calculate descriptive statistics for only the points and assists columns in the data frame.

Conclusion

The stat.desc() function is a powerful tool that can be used to calculate descriptive statistics for variables in a data frame.

By using this function, you can easily create tables that contain a variety of statistical measures, which can be useful for analyzing and visualizing your data.

  • Invisible Function in R: A Tutorial
  • Adding Space to Columns in R
  • How to Create a Two-Way Table in R
  • Error in FUN(X[[i]] …) object X not found
  • Locate the pattern in R
  • Add text in specific location in R
  • sweep function in R
  • Clean Up Memory in R
R

Post navigation

Previous Post: How to Find the Size of a Data Frame in R
Next Post: Multiple regression model in R

Related Posts

  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • R-Change Number of Bins in Histogram R
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  • Understanding the Student’s t-Distribution in R R
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Mastering R Programming for Data Science: Tips and Tricks R

Comments (2) on “Descriptive statistics in R”

  1. Marco Besozzi says:
    August 11 at 8:52 pm

    stat_desc () NO
    stat.desc() YES

    Reply
  2. X22JILLA says:
    August 23 at 2:09 am

    Hey people!!!!!
    Good mood and good luck to everyone!!!!!

    Reply

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.

  • Split a Vector into Chunks in R R
  • Add Footnote to ggplot2 R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R
  • Convex optimization role in machine learning
    Convex optimization role in machine learning Machine Learning
  • OLS Regression in R
    OLS Regression in R R
  • How to copy files in R
    How to copy files in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme