Recent Searches
Category

Microsoft Power BI

Category

Microsoft Copilot Courses

Category

AI for Business

Category

Microsoft Windows 11 Courses

Category

Microsoft 365

Category

Microsoft Excel

Category

Excel Specialist

Category

Microsoft Project

Category

R Programming

Category

Python

Category

Power Apps

Category

SQL

Category

SharePoint

Category

Microsoft Teams

Category

Power Automate

Category

Microsoft Visio

Category

Microsoft PowerPoint

Category

Microsoft Word

Category

Microsoft Outlook

Category

Adobe InDesign Courses

Category

Adobe Photoshop Courses

Category

Adobe Illustrator Courses

Category

Adobe Premiere Pro Training

Category

Adobe After Effects Training

Category

Adobe Acrobat Courses

Category

Adobe Captivate Training

Category

Adobe Animate Training

Category

Canva Courses

Category

HTML Courses

Category

WordPress

Category

Professional Development

Category

Microsoft Access

Category

Webinars

Course

Power BI Beginner

Course

Copilot for M365

Course

Windows 11 End User Course

Course

Excel Beginner

Course

Financial Modelling

Course

Project Beginner

Course

R Programming Beginner

Course

Python Beginner

Course

Power Apps Beginner

Course

SQL Beginner

Course

SharePoint Beginner

Course

Teams Essentials

Course

Visio Essentials

Course

PowerPoint Level 1

Course

Word Intermediate

Course

Word Beginner

Course

Word Advanced

Course

Microsoft Outlook Beginner to Intermediate

Course

InDesign Lite

Course

Photoshop Lite

Course

Illustrator Training Intro

Course

Premiere Training Intro

Course

After Effects Training Intro

Course

Acrobat Essentials

Course

Captivate Training

Course

Animate Training Intro

Course

Canva AI

Course

HTML Training Intro

Course

Achieving Leadership & Success

Course

Microsoft Access Essentials

Course

Copilot for M365 On Demand

Course

Power BI Intermediate

Course

Copilot for Word

Course

ChatGPT Beginner

Course

Microsoft 365 Beginner

Course

Excel Intermediate

Course

Analysis and Dashboards

Course

Project Intermediate

Course

R Programming Intermediate

Course

Python Intermediate

Course

Power Apps Intermediate

Course

SQL Intermediate

Course

SharePoint Intermediate

Course

PowerPoint Level 2

Course

InDesign Training Intro

Course

Photoshop Training Intro

Course

Acrobat Forms

Course

Anger Management & Negotiation Skills

Course

Copilot for M365 Live Online

Course

Power BI Advanced

Course

Copilot for Excel

Course

AI Prompting Fundamentals

Course

Microsoft 365 Intermediate

Course

Excel Advanced

Course

Excel VBA

Course

Project Advanced

Course

R Programming Advanced

Course

Python Advanced

Course

Power Apps Intermediate | Power Automate + Power BI Integration

Course

SharePoint Advanced (Site Owner)

Course

InDesign Training Advanced

Course

Assertiveness & Confidence

Course

Microsoft Access Advanced

Course

Power BI DAX

Course

Copilot for PowerPoint

Course

Excel Expert

Course

Machine Learning in R

Course

Power Apps Advanced

Course

SharePoint Advanced (Document Governance)

Course

Building Resilience

Course

Copilot for Outlook and Teams

Course

Coaching and Mentoring

Course

Illustrator Training Advanced

Course

Premiere Basics Training

Course

Advanced After Effects Training

Course

Canva Beginners

Course

WordPress Essentials

Course

Communications

Course

AI for Business Leaders and Managers

Course

Photoshop Training Advanced

Course

Advanced Premiere Training

Course

Canva Intermediate

Course

Communications & Quality Client Service Training

Course

InDesign Interactivity Training

Course

Canva Advanced

Course

Critical Thinking and Problem Solving

Course

InDesign Accessibility Training

Course

Cultural Diversity in the Workplace

Course

Microsoft Outlook Advanced

Course

Embracing Change

Course

SQL Advanced

Course

Growing Emotional Intelligence

Course

Planner Premium

Course

Minute Taking

Course

Excel Tables and Pivot Tables

Course

Power Automate Beginner

Course

Performance Management

Course

Data Transformation with Power Query

Course

Excel Macro Mastery

Course

Power Automate Intermediate

Course

Persuasion and Negotiation Skills

Course

Power BI Desktop Advanced Reporting

Course

Presentation Skills and Public Speaking

Course

Data Visualisation with Power BI Desktop

Course

Practical Project Management

Course

Respect, Equity and Diversity (RED)

Course

Resumé Writing and Interview Skills

Course

Stress Management

Course

Team Leadership, Management and Development

Course

Time Management Intensive

Course

Train the Trainer

Course

Write Effective Business Documents

Course

Dealing with Difficult People

Course

Managing Difficult Conversations

Course

Managing the Virtual Workplace

Course

Customer Service Training

Course

Technical Writing

Learn to simplify repetitive actions in R

Tamara Shatar | Mar 02, 2021
Binary Image

Apply Functions in R

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. For beginners, we recommend the R Basics course



R Programming Courses

Trusted Nationwide by Leading Organisations

at Nexacu, we're proud to be the trusted training partner for hundreds of leading organisations accross Australia and New Zealand. From government departments to top corporates, we help teams upskill and succeed everyday

  • 400+ companies rely on Nexacu for team training
  • Trusted by federal, state, and local government agencies
  • Delivering training across 9 countries

Why Nexacu?

Books Icon

Step by Step Courseware

Custom workbook included with a step by step exercises

Facility Image 2
Facility Image 3
Facility Image 1

Refresh Icon

Free Refresher

Resit your course for free within 6 Months

More than 1,300 Business trust Nexacu

Trusted by Business

Procured by Government

Procured by Goverment

Reviews Not Found

Valued by Individuals