Anonymous Functions: Is There A lambda() Function In R?

If you want to create anonymous functions, you might ask, “Is there a lambda() function in R?” Here is your answer.

Is There A lambda() Function In R?

Like many other programming languages, R does support anonymous functions. But the implementation isn’t lambda().

Name functions are attached to an identifier, which you can use to invoke that said function using that name. Most functions you encounter in R are named functions. That identifier allows you to call the function many times in the program in a simple way.

But you can use function() to create a function in R without assigning any name to it too.

That function becomes an anonymous function. This feature is useful when you don’t plan to recall that function again in your program. The most common application of anonymous functions is to pass a function as an argument to another function.

For instance, the sapply() function in R allows you to apply a function to every element of a vector, list, or data frame. You will need to provide two arguments: the function and the object you want to run that function on.

Let’s say you have a vector of every integer from 1 to 10, and you want to calculate the square of every element in that vector. You can do this by defining a squaring function first and passing its name to sapply():

v <- c(1:10)
f <- function(x) {
     return(x * x)
}

sapply(v, f)
 [1] 1 4 9 16 25 36 49 64 81 100

There is a small issue with this approach. The function f you have defined gets used only one. But by giving it an identifier, it will continue to exist for the rest of your R session unless you delete it. It isn’t optimal and can cause big trouble if you keep a huge number of one-time functions like that in the memory.

This is when you should consider using an anonymous function instead. There are a few to do this in R.

Instead of assigning the function to a name, you can define it right in the function sapply() like this:

sapply(v, function(x) x * x)

[1] 1 4 9 16 25 36 49 64 81 100

This way, you still use function() to define your function. However, it is passed straight to sapply() and isn’t kept in the R session afterward. You don’t need a separate expression and identifier for it, either.

With anonymous functions (also known as lambda functions or lambda expressions), your code will become more concise and use less memory.

The effect in this particular example may be negligible. But this practice can make a night and day difference in more resource-heavy programs.

From version 4.1, you can achieve the same thing with the \() shorthand as well. R will interpret \(x) as function(x), making your function declarations more succinct.

sapply(v, \(x) x * x)

[1] 1 4 9 16 25 36 49 64 81 100

Summary

So, is there a lambda() function in R? The answer is no. But don’t worry – you absolutely can define and use anonymous functions in R with function() or other shorthands. It allows you to pass a function as an argument without keeping it in the memory.

Posted in R

Leave a Reply

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