In the previous module, you learned how to clean and transform data using R. Now, it is time to learn how to study that data and discover useful information.
Imagine a school in Ibadan has examination results for 100 students. The school wants to know the average score, the highest score, the lowest score, and how many students passed.
Instead of checking every student's result manually, you can use R to calculate these values quickly.
This process is called statistical analysis.
Statistical analysis is the process of collecting, organising, calculating, and interpreting data to understand what it means.
For example, a supermarket in Lagos can use statistical analysis to:
Statistical analysis helps people make decisions based on information rather than guesswork.
Before performing calculations, let's create a dataset containing examination scores for five students.
scores <- c(85, 72, 90, 65, 78)
print(scores)
The function c() combines the scores into a vector.
We can now use different R functions to analyse the information.
The total is the result you get when you add all the values together.
In R, you can calculate the total using the sum() function.
scores <- c(85, 72, 90, 65, 78)
total_score <- sum(scores)
print(total_score)
R adds all five examination scores and stores the answer in
total_score.
Real-life example: A shop owner can use sum()
to calculate the total sales made during a day.
The average tells you the typical value in a group of numbers. In statistics, this is called the mean.
To calculate the average examination score, use:
average_score <- mean(scores)
print(average_score)
R adds all the scores and divides the total by the number of scores.
The formula for calculating the mean is:
Mean = Sum of all values รท Number of values
For example, if five students score a total of 390 marks, their average score is 390 divided by 5, which equals 78.
Sometimes, you want to identify the largest or smallest value in a dataset.
The max() function finds the highest value.
highest_score <- max(scores)
print(highest_score)
The min() function finds the lowest value.
lowest_score <- min(scores)
print(lowest_score)
These functions are useful when comparing examination results, business sales, customer payments, or product prices.
The median is the middle value in a dataset after the numbers have been arranged from the smallest to the largest.
For example, consider these examination scores:
scores <- c(65, 72, 78, 85, 90)
median_score <- median(scores)
print(median_score)
The middle value is 78, so the median score is 78.
If a dataset has an even number of values, R calculates the median by finding the average of the two middle values.
numbers <- c(10, 20, 30, 40)
median(numbers)
The two middle values are 20 and 30. Their average is 25.
The mode is the value that appears most frequently in a dataset.
For example:
scores <- c(70, 80, 80, 90, 80, 75, 70)
In this example, 80 appears three times. Therefore, 80 is the mode.
Unlike mean and median, base R does not provide a simple statistical
mode() function for finding the most frequently occurring value.
The following code can help you find it:
scores <- c(70, 80, 80, 90, 80, 75, 70)
frequency <- table(scores)
mode_score <- names(frequency)[which.max(frequency)]
print(mode_score)
The table() function counts how often each value appears,
while which.max() identifies the highest frequency.
Range shows the difference between the highest and lowest values in a dataset.
The formula is:
Range = Highest Value - Lowest Value
highest_score <- max(scores)
lowest_score <- min(scores)
score_range <- highest_score - lowest_score
print(score_range)
Range helps you understand how far apart the largest and smallest values are.
Two important statistical measurements are variance and standard deviation. They help you understand how spread out values are.
Variance measures how far values are spread around the mean. A larger variance generally means the values are more spread out.
variance_score <- var(scores)
print(variance_score)
R's var() function calculates sample variance by default.
Standard deviation measures how much values typically vary from the mean. It is expressed in the same units as the original data.
standard_deviation <- sd(scores)
print(standard_deviation)
A small standard deviation means the values are relatively close to the mean. A large standard deviation means they are more spread out.
Sometimes, you need to know how many records are contained in a dataset.
R provides the length() function for counting the number of
elements in a vector.
scores <- c(85, 72, 90, 65, 78)
number_of_students <- length(scores)
print(number_of_students)
The table() function can also count how frequently different
categories or values appear.
grades <- c("A", "B", "A", "C", "B", "A")
table(grades)
This tells you how many times each grade appears.
Sometimes, datasets contain missing information.
R represents missing values using NA.
Consider this example:
scores <- c(85, 72, NA, 90, 65)
mean(scores)
The result will be NA because one of the scores is missing.
You can tell R to ignore missing values when calculating the average.
mean(scores, na.rm = TRUE)
The argument na.rm = TRUE means that missing values should
be removed from the calculation.
You can also use it with functions such as sum(),
max(), and min().
sum(scores, na.rm = TRUE)
max(scores, na.rm = TRUE)
min(scores, na.rm = TRUE)
In real projects, information is usually stored in data frames rather than individual vectors.
Let's create a student results dataset:
students <- data.frame(
Name = c("Ada", "Tunde", "Chika", "Emeka", "Bola"),
Age = c(13, 14, 13, 15, 14),
Score = c(85, 72, 90, 65, 78)
)
print(students)
Now calculate the average examination score:
mean(students$Score)
Find the highest score:
max(students$Score)
Find the lowest score:
min(students$Score)
Calculate the median score:
median(students$Score)
Display a statistical summary of the entire dataset:
summary(students)
The dollar sign $ allows you to access a particular column
inside a data frame.
Imagine a supermarket in Lagos wants to understand its daily sales. It records the following information:
sales <- data.frame(
Product = c("Rice", "Beans", "Garri", "Oil", "Sugar"),
Quantity = c(5, 3, 4, 2, 6),
Amount = c(40000, 13500, 12000, 12000, 9000)
)
print(sales)
Calculate the total sales:
sum(sales$Amount)
Calculate the average sales value:
mean(sales$Amount)
Find the highest sales value:
max(sales$Amount)
Find the lowest sales value:
min(sales$Amount)
Calculate the total quantity of products sold:
sum(sales$Quantity)
These calculations help the supermarket understand its performance and make better business decisions.
| Function | Purpose |
|---|---|
sum() | Calculates the total. |
mean() | Calculates the average. |
median() | Finds the middle value. |
max() | Finds the highest value. |
min() | Finds the lowest value. |
range() | Returns the minimum and maximum values. |
var() | Calculates sample variance. |
sd() | Calculates sample standard deviation. |
length() | Counts the elements in a vector. |
table() | Counts how frequently values occur. |
summary() | Provides a statistical summary. |
A school in Ibadan wants you to analyse the examination results of its students. Use R to complete the following activities.
Create this dataset:
scores <- c(65, 80, 75, 90, 55, 85, 70, 95, 60, 80)
Complete these tasks:
In this module, you have learned how to:
sum().mean().You have now learned how to use R to calculate and understand important information in a dataset.
But numbers alone do not always make information easy to understand. Sometimes, you need charts and graphs to explain what the data is telling you.
In the final module, Data Visualisation and Practical Project with R, you will learn how to create charts, visualise information, and complete a simple data analysis project using R.