R: How to Apply a Function Over a Vector


Often you may want to apply a function to each element of a vector in R.

The easiest way to do so is by using the sapply() function from base R, 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.

The following examples show how to use the sapply() function 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 Apply a Function Over a Vector 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 performs the following task:

  • First, add 3 to the number.
  • Then, multiply the number by 5.

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

#apply function to each element of vector
sapply(my_vector, function(x) return ((x+3)*5))

[1]  20  30  30  35  45  55  75  90 110 120

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 = 20
  • Second value: (3+3) * 5 = 30
  • Third value: (3+3) * 5 = 30
  • Fourth value: (4+3) * 5 = 35

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:

#apply specific function to each column of data frame
sapply(my_df, function(x) return ((x+3)*5))

      col1 col2
 [1,]   20   15
 [2,]   30   15
 [3,]   30   25
 [4,]   35   30
 [5,]   45   30
 [6,]   55   35
 [7,]   75   40
 [8,]   90   40
 [9,]  110   50
[10,]  120   55

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.

Note that the sapply() function is able to work smoothly with data frames because it returns vectors as a result. Since each data frame column is treated as a vector, the sapply() function is able to seamlessly perform operations on multiple columns of a data frame.

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 *