How to Store and Calculate Values in R
How to Look at Data Margins and Proportions in R
How to Change Values in a Vector in R

How to Apply Functions to a Vector in R

The apply() function works on anything that has dimensions in R, but what if you don’t have dimensions? For that, you have two related functions from the apply family at your disposal sapply() and lapply(). The l in lapply stands for list and the s in sapply stands for simplify.

The two functions work basically the same — the only difference is that lapply() always returns a list with the result, whereas sapply() tries to simplify the final object if possible.

Both sapply() and lapply() consider every value in the vector to be an element on which they can apply a function. Many functions in R work in a vectorized way, so there’s often no need to use this.

The switch() function, however, doesn’t work in a vectorized way. Consider the following basic example:

> sapply(c('a','b'), switch, a='Hello', b='Goodbye')
        a         b
  "Hello" "Goodbye"

The sapply() call works very similar to the apply() call, although you don’t have an argument that specifies the index. Here’s a recap:

  • The first argument is the vector on which values you want to apply the function — in this case, the vector c('a', 'b').

  • The second argument is the name of the function — in this case, switch.

  • All other arguments are simply the arguments you pass to the switch function.

The sapply() function now takes first the value 'a' and then the value 'b' as the first argument to switch(), using the arguments a='Hello' and b='Goodbye' each time as the other arguments. It combines both results into a vector and uses the values of c('a', 'b') as names for the resulting vector.

The sapply() function has an argument USE.NAMES that you can set to FALSE if you don’t want sapply() to use character values as names for the result.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Add Row and Column Totals in R
How to Create Subgroups of Data in R
How to Extract Data Test Results with R
How to Use Quantile Plots to Check Data Normality in R
How to Create an Array in R