How to Use apply in R to apply a function to a matrix or data frame - ProgrammingR (2024)

The R programming language is one of the most popular options among people working in data science. And one of the reasons for this popularity stems from R’s relationship to data manipulation. The base R language gives you a variety of powerful ways to work with large collections of numbers. Along with this you’ll also find many different data types that let you elegantly manipulate, import, and export information. The apply function and its related components are one of the best examples of how R excels in this area. And you’ll soon see just how easy it is to use apply in R to direct a function to a matrix, data frame, or other vector.

What Are the Apply-Related Functions?

Before looking at solutions which use the apply function we need to first consider exactly why it’s such an important part of the base R language. And this gets to the heart of why R is such a popular choice for research and statistical work.

Almost all programming languages can work with massive amounts of information. But there’s always larger questions to ask at the beginning of a project. It’s important to consider how much work will go into creating functions to manipulate those large data sets. And it’s equally important to consider overall processing speed and efficiency. The absolute ideal is a programming language which will let you quickly write human readable code that will run at an equally impressive speed. The R programming language provides a number of coding shortcuts to make that ideal a reality.

R’s basic syntax is relatively high-level and easy to work with. And R also has methods that can optimize otherwise processor and memory-heavy procedures. One of the key examples of this optimization is found in R’s relationship to its data frame, matrix, list, and similar collections. In many other languages you’d need to manually iterate through every element within a collection. But most of R’s functions can be classified as a vectorized function. This term describes a function that can act on a collection of elements as a whole or in part with no need to manually loop through them.

Vectorization might not seem very important at first. But it’s part of what qualifies R as one of the best of the best for stasticial work. Manually iterating over a huge numerical set can be a computationally intense task. And it also adds a lot of bloat to your code. Vectorized functions reduce the computational strain while also making it easier to design those procedures.

Starting Out With the Basics of Apply

The benefits of apply can be more easily seen by looking at some examples. We can begin by demonstrating how we’d write out a basic addition procedure in a non-vectorized system. Take a look at the following code.

ourDataFrame <- data.frame(
first = c (1,2,3,4,5,6,7,8,9,10),
second = c(11,12,13,14,15,16,17,18,19,20),
third = c(21,22,23,24,25,26,27,28,29,30),
fourth = c(31,32,33,34,35,36,37,38,39,40),
fifth = c(2,2,2,2,2,2,2,2,2,2))

y <- 0
for (x in ourDataFrame$fifth) {
y <- y + x
}
print(y)

In this example we’ll add up all of the numbers in the fifth column of a data frame. We begin by declaring a new frame called ourDataFrame and populate it with five columns. Next, we create the y variable and set it to 0. We proceed into a for loop that specifically works through the fifth column of ourDataFrame. Each iteration adds the value of the current point in our column to the y variable. Finally, we print out the current value of y.

This method of working through a set is obviously functional. But it’s highly unoptimized. And most importantly, it’s not taking advantage of R’s vectorized nature. Now take a look at the following code to see a more concise and optimised approach.

ourDataFrame <- data.frame(
first = c (1,2,3,4,5,6,7,8,9,10),
second = c(11,12,13,14,15,16,17,18,19,20),
third = c(21,22,23,24,25,26,27,28,29,30),
fourth = c(31,32,33,34,35,36,37,38,39,40),
fifth = c(2,2,2,2,2,2,2,2,2,2))
print(apply(ourDataFrame[5], 2, sum))

We begin by defining ourDataFrame in the same way as the previous example. But all of the functionality from the rest of the example’s code is now wrapped up into a single line. The print statement encapsulates an example of how we can use apply. In this case we call apply and pass the fifth element of ourDataFrame. We then supply two additional arguments. These are the number 2 and the word sum.

The number 2 is fed into apply’s margin variable. Margin is just shorthand for whether we’re working with rows, columns, or both. If we supply a 1 then apply will work on rows. If we supply 2 then apply works with columns. Note that margin can be told to work with both rows and columns. But contrary to what you might expect, this doesn’t mean passing the number 3. We instead need to essentially use both 1 and 2. We could do so by passing c(1,2) as our margin argument. But in this case we’re only concerned with the fifth column so we’ll pass 2 as the argument.

Finally, we use sum as the last argument passed to apply. In this case sum refers to R’s sum function. Note that this can be any R function. And that even includes functions that you’ve personally written.

This concept will also work with other R collections. For example, the following code shows how we can adapt this idea to use a matrix instead of a data frame.

ourMatrix <- matrix(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, 31,32,33,34,35,36,37,38,39,40, 2,2,2,2,2,2,2,2,2,2 ), nrow = 10, ncol = 5)
print( apply(ourMatrix[,5,drop=F], 2, sum) )

We start out by assigning a matrix to ourMatrix that consists of ten rows and five columns. We use the same values as in the previous example. The apply once again calls sum and is wrapped in a print command. Likewise, we’re still using 2 to indicate that we want to work on a column. The main difference simply comes from how we point to a location in a different data type. In this case we’re specifying the fifth column, while also signaling that we want to preserve the matrix structure through drop=F.

In both examples we’re essentially getting the good parts of a for loop without the deficit of performance hits and overly verbose code blocks. However, apply in R is actually just one function in a set of similar options. And we can get even more power out of the language by matching functions to our needs.

Other Variations on Apply

We can use apply on almost any of R’s collection types. However, we can optimize our code by matching a particular data type to the appropriate varient of apply. For example, the earlier code that worked on a matrix could be better paired with the mapply function. Likewise, if we were working with a list we could use the lapply function. Or the tapply function if we needed to work on a sebset of our collections that’s broken down by a supplied value. Take a look at the following code to see this concept in action.

ourDataFrame <- list(
first = c (1,2,3,4,5,6,7,8,9,10),
second = c(11,12,13,14,15,16,17,18,19,20),
third = c(21,22,23,24,25,26,27,28,29,30),
fourth = c(31,32,33,34,35,36,37,38,39,40),
fifth = c(2,2,2,2,2,2,2,2,2,2))

ourLAppliedValue <- lapply(ourDataFrame[5], sum)
print(ourLAppliedValue)
print(typeof(ourLAppliedValue))

ourSAppliedValue <- sapply(ourDataFrame[5], sum)
print(ourSAppliedValue)
print(typeof(ourSAppliedValue))

We start out with the same variables from our initial example. But this time around we define them as a list rather than a data frame. Next, we use the lapply function in the same way that we had previously used apply. We assign the result to ourLAppliedValue and print out its current value and type. Then we repeat the process using the sapply function.

We wind up with the same summed value in both of these cases. But when we use lapply our results are presented in list form because the function defaults to lists. While sapply tries to match output to input. Sapply outputs a double because sum has reduced the list to a number value rather than a collection of numbers.

Need more options? Check Out The articles below…

  • How To Use the apply function (matrix or data frame)
  • How To Use the sapply function (simplified version of lapply)
  • How To Use the lapply function (list or vector)
  • How To Use the mapply function (applying a function to multiple lists or vectors)
  • How To Use the tapply function (levels of a factor)
  • While Loops
  • For Loops
  • Creating Anonymous Functions in R
How to Use apply in R to apply a function to a matrix or data frame - ProgrammingR (2024)

FAQs

How do I apply a function to a data frame in R? ›

apply() lets you perform a function across a data frame's rows or columns. In the arguments, you specify what you want as follows: apply(X = data. frame, MARGIN = 1, FUN = function.

What is apply() in R? ›

The apply() function lets us apply a function to the rows or columns of a matrix or data frame. This function takes matrix or data frame as an argument along with function and whether it has to be applied by row or column and returns the result in the form of a vector or array or list of values obtained.

How do you apply a function to each row of a matrix in R? ›

the apply function looks like this: apply(X, MARGIN, FUN).
  1. X is an array or matrix (this is the data that you will be performing the function on)
  2. Margin specifies whether you want to apply the function across rows (1) or columns (2)
  3. FUN is the function you want to use.

How do you apply a function to each element in R? ›

lapply() is a function in R that applies a given function (specified by the FUN argument) to each element of a list or vector. It generates a list where each element represents the outcome of applying the tolower() function to each element within the given vector.

How to create data frame with function in R? ›

To create an R data frame use data. frame() function and then pass each of the vectors you have created as arguments to the function.

What does data frame () do in R? ›

The function data. frame() creates data frames, tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R's modeling software.

What is the apply () function? ›

The apply() method is one of the most common methods of data preprocessing. It simplifies applying a function on each element in a pandas Series and each row or column in a pandas DataFrame.

How to apply a function to multiple columns in R? ›

Applying a Function to Multiple Columns

For this, we can use the mutate_all function from the dplyr package. The mutate_all function takes a data frame as input and applies a function to all columns. Let's say we have a data frame df with three columns, and we want to apply the logarithm function to all columns.

What is apply command? ›

The apply command runs a command string specified by the CommandString parameter on each specified value of the Parameter parameter in turn. Normally, Parameter values are chosen individually; the optional -Number flag specifies the number of Parameter values to be passed to the specified command string.

How do I apply a function to every row of a column in R? ›

Instead of using apply on a single column (a Series ), we can also use apply on the whole DataFrame. The default axis for applying the function is axis = 0 (applying the function to each column). To apply the function to each row, we specify axis = 1 .

How to apply the same function to all rows and columns of a matrix? ›

Using the apply() Function
  1. m is the matrix.
  2. dimcode is the dimension, equal to 1 if the function applies to rows or 2 for columns.
  3. f is the function to be applied.
  4. fargs is an optional set of arguments to be supplied to f .

How do I add a row to a matrix in R? ›

Append a row into the matrix by using rbind()

The function rbind() stands for row bind. It is one of the function used to append a row into a matrix in an efficient manner. This function can have ability to combine data sets such as matrices, data frames, and vectors.

How to apply functions to a list? ›

In Python, the map() function can be used to apply a specific function to each element in a list. The map() function takes in two arguments: a function and an iterable (such as a list).

Which function is used to apply a function to each element of a vector and simplify the results in R? ›

lapply returns a list of the same length as X , each element of which is the result of applying FUN to the corresponding element of X . sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify = "array" , an array if appropriate, by applying simplify2array() .

How do you write a function in R with multiple inputs? ›

If a function should have more than one argument, list them in the function signature, separated by commas. To solve this exercise, you need to know how to specify sampling weights to sample() . Set the prob argument to a numeric vector with the same length as x .

How to apply a function across all columns in R? ›

Applying a Function to Multiple Columns

For this, we can use the mutate_all function from the dplyr package. The mutate_all function takes a data frame as input and applies a function to all columns.

How to call a function in R? ›

Calling a Function in R

To do so, we just put the punction name and added the necessary arguments inside the parenthesis. In R, function arguments can be passed by position, by name (so-called named arguments), by mixing position-based and name-based matching, or by omitting the arguments at all.

How do I access an element of a data frame in R? ›

Similar to vectors and matrices, you select elements from a data frame with the help of square brackets [ ] . By using a comma, you can indicate what to select from the rows and the columns respectively. For example: my_df[1,2] selects the value at the first row and second column in my_df .

Which R function can be used to make changes to a data frame? ›

Which R function can be used to make changes to a data frame? The mutate() function can be used to make changes to a data frame.

Top Articles
Adult Look Sacramento
Bedding Tommy Hilfiger - Shop online and save up to 4% | UK
What Is Single Sign-on (SSO)? Meaning and How It Works? | Fortinet
Aberration Surface Entrances
Craigslist Pets Longview Tx
4-Hour Private ATV Riding Experience in Adirondacks 2024 on Cool Destinations
Goodbye Horses: The Many Lives of Q Lazzarus
Unitedhealthcare Hwp
Mail Healthcare Uiowa
Barstool Sports Gif
When Is the Best Time To Buy an RV?
Campaign Homecoming Queen Posters
Lantana Blocc Compton Crips
Find your energy supplier
The Binding of Isaac
Walthampatch
Craigslist Deming
Eka Vore Portal
Craigslist Farm And Garden Cincinnati Ohio
Troy Bilt Mower Carburetor Diagram
Craigslist Red Wing Mn
Ally Joann
Long Island Jobs Craigslist
Amazing deals for Abercrombie & Fitch Co. on Goodshop!
Icivics The Electoral Process Answer Key
Hdmovie2 Sbs
Wood Chipper Rental Menards
Penn State Service Management
Superhot Free Online Game Unblocked
Srjc.book Store
Elanco Rebates.com 2022
Issue Monday, September 23, 2024
Mkvcinemas Movies Free Download
Http://N14.Ultipro.com
No Hard Feelings Showtimes Near Tilton Square Theatre
John F Slater Funeral Home Brentwood
Hermann Memorial Urgent Care Near Me
Aliciabibs
Avance Primary Care Morrisville
Empire Visionworks The Crossings Clifton Park Photos
Raisya Crow on LinkedIn: Breckie Hill Shower Video viral Cucumber Leaks VIDEO Click to watch full…
Labyrinth enchantment | PoE Wiki
Trizzle Aarp
MSD Animal Health Hub: Nobivac® Rabies Q & A
Seminary.churchofjesuschrist.org
Sarahbustani Boobs
The Horn Of Plenty Figgerits
Maplestar Kemono
DL381 Delta Air Lines Estado de vuelo Hoy y Historial 2024 | Trip.com
Powah: Automating the Energizing Orb - EnigmaticaModpacks/Enigmatica6 GitHub Wiki
Edt National Board
Hy-Vee, Inc. hiring Market Grille Express Assistant Department Manager in New Hope, MN | LinkedIn
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6555

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.