1. Home
  2. Dimitri Bianco

Feb 8

Why R's apply() function beats colMeans() for custom code

9 min → 1:42

Overview

Dimitri Bianco explains the syntax of the apply function in R, showing how to calculate matrix row and column means while fixing common output formatting issues.

3 things to know

Bianco says apply runs custom functions without complex loops

Dimitri Bianco says that while specific functions like colMeans exist for simple math, learning the apply function lets programmers run any custom calculation across a dataset without writing complex loops.

▶ Watch at 8:39

Bianco: The apply function takes four main inputs

Dimitri Bianco explains that the apply function in R uses a specific order of inputs: the data table, a number for rows or columns, the calculation to run, and optional settings.

▶ Watch at 5:06

Bianco: Passing 1 runs across rows and 2 picks columns

Dimitri Bianco explains that the dimension argument in R tells the apply function which direction to work, with 1 running calculations across rows and 2 running them across columns.

More

A critical distinction in R is the dimension argument.

▶ Watch at 1:29

Also in this episode

Bianco wraps apply results in matrix to keep headers

Dimitri Bianco demonstrates wrapping the output of the apply function inside a matrix function to keep clean row and column headers when the result returns a simple list of numbers.

More

When `apply()` returns a vector, it may lose the structural context of the original data (e.g., returning a simple list of numbers rather than a column within a dataframe).

Quotes

“Iterating through every column if you're trying to apply something is a huge headache... often I'm wanting to do something a little bit custom, a little bit different... so I'm going to build my own function, and then I want to pass that and do every single column.”

▶ Watch at 8:39

“I know there is a function called colMeans()... and this function will do the exact same thing, but doing a mean is a really simple function... so that we can actually see it, test it, understand it, and then we can kind of build up on that complexity as needed.”

▶ Watch at 1:29

“We're going to take our apply function, we're going to pass it Z as the matrix. This time the dimcode... is going to be 1 [for rows].”

▶ Watch at 5:06

Full summary

Episode Overview

  • Subject: This episode serves as a foundational tutorial on the apply() function in the R programming language, specifically focusing on its basic application to matrices.
  • Structure: The host breaks down the syntax of the function, demonstrates how to calculate means across both columns and rows of a matrix, and addresses common formatting issues that arise during the output.
  • Relevance: Ideal for beginners in R or data analysis, this video bridges the gap between simple pre-built functions (like colMeans) and custom iterations, setting the stage for more complex financial modeling in future episodes.

Key Concepts

  • The apply() Syntax: The function follows the structure apply(m, dimcode, f, fargs), where m is the matrix, dimcode specifies the dimension (1 for rows, 2 for columns), f is the function to be applied, and fargs are optional arguments for that function.
  • Vectorization vs. Iteration: While specific functions like colMeans() or rowMeans() exist for simple calculations, learning apply() is crucial because it allows programmers to broadcast any custom function across a dataset without writing complex loops.
  • Dimension Codes (Dimcode): A critical distinction in R is the dimension argument. Passing 1 instructs R to apply the function across rows, while passing 2 instructs it to apply the function across columns.
  • Data Formatting Challenges: When apply() returns a vector, it may lose the structural context of the original data (e.g., returning a simple list of numbers rather than a column within a dataframe). The host demonstrates how to wrap the output in a matrix() function to maintain a clean, readable structure with proper row and column headers.

Quotes

  • At 1:29 - "I know there is a function called colMeans()... and this function will do the exact same thing, but doing a mean is a really simple function... so that we can actually see it, test it, understand it, and then we can kind of build up on that complexity as needed." - explains why the tutorial uses a simple example to teach a powerful, flexible concept.
  • At 5:06 - "We're going to take our apply function, we're going to pass it Z as the matrix. This time the dimcode... is going to be 1 [for rows]." - clarifies the specific argument change required to switch from column-based to row-based calculations.
  • At 8:39 - "Iterating through every column if you're trying to apply something is a huge headache... often I'm wanting to do something a little bit custom, a little bit different... so I'm going to build my own function, and then I want to pass that and do every single column." - highlighting the real-world utility of apply() for custom data processing over pre-built functions.

Takeaways

  • Use apply() when you need to run a custom function across every row or column of a dataset, rather than relying solely on built-in functions like rowMeans which are limited in scope.
  • Nest your apply() function inside a matrix() function to force the output into a specific shape (e.g., specifying nrow or ncol), ensuring your data remains readable and structurally consistent with the original dataset.
  • When processing large datasets with potential data quality issues (like economic time series with rebasing errors), use apply() to iterate plotting or checking functions across hundreds of columns to quickly visualize anomalies.

Watch the full episode 9 min

Read along with the audio summary

Iterating through every column of a dataset in the R programming language can be a huge headache, as the host points out, especially when you want to do something custom. In this R programming tutorial episode, which came out on February 8, 2026, the host gives a foundational lesson on the apply function for matrices. By the end, you will know how to run custom calculations across rows and columns and keep your output formatted cleanly.

Learn a simple four part structure to control how R processes your data tables. You give it your matrix, a dimension code, the function you want to run, and any optional arguments for that function.

This four part setup unlocks a way to broadcast any custom function across your dataset without writing complex loops. R already has built in shortcuts for simple averages, like column means or row means. However, the host explains that practicing apply with a simple average lets you see and test how it works before you build your own custom functions for every column.

You can switch your calculation from columns to rows by changing a single number. Passing the number one tells R to apply your function across rows, while passing the number two applies it across columns.

Running apply can strip away your table structure, but a simple wrapper fixes it. When apply returns a vector, which is a simple list of numbers, it may lose the structural context of your original dataframe, or data table. To fix this, the host shows how to wrap the output inside a matrix function, keeping a clean, readable structure with proper row and column headers.

The apply function lets you run custom checks across every row or column without complex loops. When processing large datasets like economic time series with rebasing errors, use apply to run plotting or checking functions across hundreds of columns to quickly visualize anomalies.

More from Dimitri Bianco

Related Episodes