0
0
R Programmingprogramming~5 mins

Descriptive statistics in R Programming

Choose your learning style9 modes available
Introduction

Descriptive statistics help us understand data by summarizing it with simple numbers and tables.

To find the average score of students in a class.
To see how spread out daily temperatures are in a city.
To check the most common category in survey answers.
To quickly summarize sales numbers for a product.
To compare heights of people in two groups.
Syntax
R Programming
mean(x)
median(x)
var(x)
sd(x)
summary(x)
table(x)

x is your data vector or column.

summary() gives min, 1st quartile, median, mean, 3rd quartile, max.

Examples
Calculate the average of numbers in x.
R Programming
x <- c(2, 4, 6, 8, 10)
mean(x)
Find the middle value in x.
R Programming
x <- c(2, 4, 6, 8, 10)
median(x)
Calculate variance and standard deviation to see data spread.
R Programming
x <- c(2, 4, 6, 8, 10)
var(x)
sd(x)
Count how many times each fruit appears.
R Programming
x <- c('apple', 'banana', 'apple', 'orange', 'banana')
table(x)
Sample Program

This program calculates basic descriptive statistics for a small set of numbers.

R Programming
data <- c(5, 7, 8, 9, 10, 10, 12, 15, 18, 20)

cat('Mean:', mean(data), '\n')
cat('Median:', median(data), '\n')
cat('Variance:', var(data), '\n')
cat('Standard Deviation:', sd(data), '\n')
cat('Summary:', '\n')
print(summary(data))
OutputSuccess
Important Notes

Missing values (NA) can cause errors; use na.rm=TRUE inside functions to ignore them.

Variance and standard deviation measure spread; variance is in squared units, standard deviation in original units.

Summary

Descriptive statistics summarize data with simple numbers.

Common measures include mean, median, variance, and standard deviation.

Use summary() for a quick overview of numeric data.