R: How to Use sapply() Function with Multiple Arguments


One of the easiest ways to apply a function to each element of a vector in R is by using the sapply() function, which is designed to perform this exact task.

The sapply() function uses the following basic syntax:

sapply(X, FUN)

where:

  • X: A vector or another object in R
  • FUN: The function to be applied to each element of x

This function returns a vector as a result in which the length of the output vector is the same as the input vector.

Often you may want to use the sapply() function with multiple arguments.

You can use the following basic syntax to do so:

#define function
my_function <- function(var1,var2,var3){
  var1*var2*var3
}

#apply function to vector using multiple arguments
sapply(my_vector, my_function, var2=3, var3=5)

The following examples show how to use the sapply() function to in practice.

Note: The sapply() function comes built-in with R so you can use it without installing or loading any external packages.

Example: How to Use sapply() Function with Multiple Arguments in R

Suppose that we create a vector named my_vector in R:

#create vector
my_vector <- c(1, 3, 3, 4, 6, 8, 12, 15, 19, 21)

Now suppose that we would like to apply a function to each element in this vector that multiplies each element by three specific numbers.

We can use the following syntax with the sapply() function to do so:

#create function with multiple arguments
my_function <- function(var1,var2,var3){ var1*var2*var3 }

#apply function to each element of vector
sapply(my_vector, my_function, var2=3, var3=5)

[1]  15  45  45  60  90 120 180 225 285 315

Notice that the sapply() function applies the function that we specified to each element of the vector and outputs a resulting vector with the same length.

Here is how each value in the resulting vector was calculated:

  • First value: 1 * 3 * 5 = 15
  • Second value: 3 * 3 * 5 = 45
  • Third value: 3 * 3 * 5 = 45
  • Fourth value: 4 * 3 * 5 = 60

And so on.

It’s worth noting that the sapply() function can also be used to apply a function to multiple columns of a data frame at once since each individual column is treated as a vector.

For example, suppose we have a data frame with two columns:

#create data frame
my_df <- data.frame(col1=c(1, 3, 3, 4, 6, 8, 12, 15, 19, 21),
                    col2=c(0, 0, 2, 3, 3, 4, 5, 5, 7, 8))
                    
#view data frame
my_df

   col1 col2
1     1    0
2     3    0
3     3    2
4     4    3
5     6    3
6     8    4
7    12    5
8    15    5
9    19    7
10   21    8

Now suppose that we would like to apply the same function to each column of the data frame by using the sapply() function.

We can use the following syntax to do so:

#create function with multiple arguments
my_function <- function(var1,var2,var3){ var1*var2*var3 }

#apply specific function to each column of data frame
sapply(my_df, my_function, var2=3, var3=5)

      col1 col2
 [1,]   15    0
 [2,]   45    0
 [3,]   45   30
 [4,]   60   45
 [5,]   90   45
 [6,]  120   60
 [7,]  180   75
 [8,]  225   75
 [9,]  285  105
[10,]  315  120

Notice that the sapply() function was able to apply this specific function to each column of the data frame.

Note that in this example we used a data frame with only two columns but you can use the sapply() function to apply a function to a data frame with any number of columns.

Also note that the sapply() function is able to work with data frames without any errors because it returns vectors as a result.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Write a Repeat Loop in R
How to Append Values to a Vector Using a Loop in R
How to Create a Nested For Loop in R
How to Use While Loops in R

Leave a Reply

Your email address will not be published. Required fields are marked *