Apply Family Functions in R
Welcome to the Intermediate Level of the Dataplexa R Programming course. In this lesson, you will learn about the Apply Family, one of the most powerful features in R.
Apply functions help you perform operations on data without writing long and complex loops. They make your code shorter, cleaner, and easier to understand.
Why Use Apply Functions?
Before apply functions, you learned how to use for loops.
While loops work well, they can become lengthy and harder to read.
Apply functions solve this problem by applying a function to data in a simple and efficient way.
Overview of Apply Family
The most commonly used apply family functions are:
- apply() – works on rows or columns of matrices and data frames
- lapply() – returns output as a list
- sapply() – returns a simplified result
- tapply() – applies a function based on groups
Each function has a specific purpose, which you will understand step by step.
Using apply()
The apply() function is commonly used with matrices and data frames.
It applies a function across rows or columns.
data <- matrix(c(10, 20, 30, 40, 50, 60), nrow = 2)
apply(data, 1, sum) # Row-wise sum
apply(data, 2, mean) # Column-wise mean
Here, 1 means rows and 2 means columns.
Using lapply()
The lapply() function applies a function to each element of a list.
It always returns the output as a list.
numbers <- list(a = 1:5, b = 6:10)
lapply(numbers, sum)
Each list element is processed separately, and results are stored in a list.
Using sapply()
The sapply() function works like lapply() but tries to simplify the output.
It usually returns a vector or matrix instead of a list.
sapply(numbers, sum)
This makes the result easier to read and use.
Using tapply()
The tapply() function applies a function based on groups.
It is very useful for grouped calculations.
scores <- c(80, 75, 90, 85, 70)
group <- c("A", "A", "B", "B", "A")
tapply(scores, group, mean)
Here, the average score is calculated for each group separately.
Comparing Loop vs Apply
Using apply functions reduces the need for manual loops.
This improves code readability and reduces the chance of errors.
📝 Practice Exercises
Exercise 1
Create a matrix and use apply() to find the maximum value of each row.
Exercise 2
Use lapply() to calculate the mean of each element in a list.
Exercise 3
Use sapply() to calculate the length of each vector in a list.
Exercise 4
Use tapply() to calculate the sum of values by category.
✅ Practice Answers
Answer 1
mat <- matrix(c(5, 10, 15, 20), nrow = 2)
apply(mat, 1, max)
Answer 2
values <- list(x = c(2, 4, 6), y = c(1, 3, 5))
lapply(values, mean)
Answer 3
sapply(values, length)
Answer 4
sales <- c(100, 200, 150, 300)
region <- c("East", "West", "East", "West")
tapply(sales, region, sum)
What’s Next?
In the next lesson, you will learn how to create custom functions in R.
This will help you write reusable and well-structured R programs.