All courses available in-class or via remote login

In R, functions from the apply family are used to apply a function repeatedly to subsets of data in a single line of short code. They are great for reporting summary statistics for each row or column of data or even for different categories within the dataset.

We refer to the apply family because there are a few different apply functions, each operating in a slightly different way. These functions are also known as functionals because they take other functions as arguments. These functions are applied to subsets of the data.

Functionals can often be used in place of writing loops. The code for an apply function is much more concise than code for a loop, which can save time spent on writing, debugging and maintaining code. Apply functions can also be faster than loops in R. But remember, where possible, take advantage of vectorisation in R! It is always the fastest option.

The examples below demonstrate the use of apply functions using the mtcars dataset, distributed with R.

Need to calculate the range of each column in a dataframe?

lapply() will return the range (minimum and maximum value) for each column as a list. Each element of the list will have the same name as the column name.

R Programming sapply

sapply() will return the same data in a simplified format, i.e. a matrix.

R Programming apply


R Programming apply syntax

Want to compare average values between categories?

The tapply() function allows you to split a vector of values by a factor (category) and then applies a function to each category subset.

R Programming apply code

In the example above, the results show the average miles per gallon achieved by cars with 4, 6 and 8 cylinders.

Working with multiple datasets?

The mapply() function is the multivariate apply function and allows you to specify multiple datasets. The function will first be applied to each element of each dataset, then to the next element of each and so on.

For example, we can use the beaver1 and beaver2 datasets distributed with R. Both are structured identically and contain body temperature measurements recorded at regular intervals.

mapply() can be used to return the range of values for each column in the combined datasets.

Apply function in R

Learn more about R Programming on our courses or by enrolling in our R certification programme.

R Programming Courses

Contact Us

What is Remote Training?

Valued by Individuals

4.72 / 5
Over 26985 Reviews
Good knowledge and very useful. -Excel Intermediate Remote Online
Liliana - Excel Intermediate, .
The instructions were clear and provided advice on pivot tables and graphs which found very helpful. -Excel Intermediate Remote Online
Dayle - Excel Intermediate, .
Very informative and easy to follow and understand. -Excel Intermediate Remote Online
Cindy - Excel Intermediate, .
Very well presented, Steven was very patient and explained things very well. was a very informative day. -Excel Intermediate Remote Online
Katherine - Excel Intermediate, .
Very informative course, well presented and I will use a lot of this information -Excel Intermediate Remote Online
Valerie - Excel Intermediate, .
Steven was a great teacher he was very patient and did everything step by step so it wasn't confusing. Would recommend this course to someone who is wanting to learn about excel. -Excel Intermediate Remote Online
Maddison - Excel Intermediate, .
Steven was very patient and went back to re-do steps if we got confused. Learnt a lot and was very informative. -Excel Intermediate Remote Online
Kim - Excel Intermediate, .
Very informative - expanded on my knowledge. Some good tips were provided regarding shortcuts. -Excel Intermediate Remote Online
Michelle - Excel Intermediate, .
Very informative course! -Excel Intermediate Remote Online
Julie - Excel Intermediate, .
Steven was a great trainer -Excel Intermediate Remote Online
Michelle - Excel Intermediate, .
Thanks a good fast paced course with a knowledgable trainer -Excel Intermediate Remote Online
Vicki - Excel Intermediate, .
Really informative course with Jason. Went at a good pace and constantly asked if we had any questions. -Excel Intermediate Brisbane
Roland - Excel Intermediate, .
Great session! Very informative. Ken was a great presenter, very patient and helpful. -Power BI Intermediate Melbourne
Donna - Power BI Intermediate, .
xlnt presenter -Excel Intermediate Brisbane
Neal - Excel Intermediate, .
Ken is a very good teacher and the whole training course is very informative and enjoyable. -Power BI Intermediate Melbourne
TUAN - Power BI Intermediate, .
Great day. Learnt a lot of new info about how to better use excel and save myself a lot of time trying to figure things out. Will reduce my workload! -Excel Intermediate Brisbane
Natalie - Excel Intermediate, .
Thought it was great! Very engaging considering completed remotely. Definitely helpful. Thank you! -Power BI Intermediate Melbourne
Lauren - Power BI Intermediate, .
Thank you for the course. Very clear and concise. I would definitely recommend the intermediate course for people with beginner experience. -Power BI Intermediate Melbourne
Matthew - Power BI Intermediate, .
Excellent course, a lot of information to take in over a day but very useful and feel confident enough to play around PBI on my own. Facilitator was very engaging and entertaining, so helped us stay tuned throughout the day. -Power BI Intermediate Melbourne
Iantha - Power BI Intermediate, .
Great pace, good examples and easy to follow. -Power BI Intermediate Melbourne
Tania - Power BI Intermediate, .

Trusted by Business

Procured by Government

Awards and Accreditations

Nexacu offices Australia

Follow us