Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda-like functions in R?

Tags:

r

lambda

I’m required to use/learn R for a new lecture at uni and I’m currently struggling a bit with its syntax. I want to plot (via curve) a simple function, but I can’t seem to get it working with an inline lambda-like function.

I’ve tried the following:

> curve( function(x) x^2 ) Error in curve(function(x) x^2) :    'expr' did not evaluate to an object of length 'n' 

When I however store the function in a variable first, it works:

> quad <- function(x) x^2 > curve( quad ) 

Is such an inline use not allowed in R? Is there any other way to make this work without defining an extra function? Thanks!

like image 351
poke Avatar asked Oct 20 '11 08:10

poke


People also ask

Is there a lambda function in R?

Lambda. r is a language extension that supports a functional programming style in R. As an alternative to the object-oriented systems, lambda. r offers a functional syntax for defining types and functions.

What is an anonymous function in R?

What are anonymous functions in R? Anonymous functions are those functions which are not assigned a variable name. These functions are also known as lambda functions (just like the ones in python) These functions are just created for the time being and are used without defining a variable name to them.

Is lambda function faster than normal function?

Being anonymous, lambda functions can be easily passed without being assigned to a variable. Lambda functions are inline functions and thus execute comparatively faster.

How do you convert a function to lambda?

To create a lambda function first write keyword lambda followed by one of more arguments separated by comma ( , ), followed by colon a ( : ), followed by a single line expression. Here we are using two arguments x and y , expression after colon is the body of the lambda function.


2 Answers

Just for completeness. You can use "lambda-like" (anonymous) functions in R but if you want to put them to immediate use, you need to enclose the function definition in parentheses or curly braces:

(function (x) x+1) (1) {function (x,y) x^y} (2,3) 

In the case of curve the first argument is either expression or a function name - but if it is a function name then it is first converted to an expression. (See first few lines in the source code of curve). So if its' not a function name, you'll need an expression – which may contain a "lambda" function:

curve((function (x) x^2)(x)) 

If you want to use a function (as opposed to its name) as the argument, you can use plot.function:

plot(function(x) x^2) 
like image 80
lebatsnok Avatar answered Oct 04 '22 13:10

lebatsnok


You have to look at the source of curve to appreciate what is happening (just type curve at the prompt and press enter).

There you can find how the expression passed is parsed.

The only way a function is discovered as being just that, is when only its name is passed along (see the is.namepart). If that is not the case, the expression is called for every x. In your case: for every x, the result is a function, which is not a happy thought for plotting...

So in short: no you cannot do what you tried, but as @ROLO indicated, you can immediately pass the function body, which will be parsed as an expression (and should contain x). If this holds multiple statements, just enclose them in curly braces.

like image 36
Nick Sabbe Avatar answered Oct 04 '22 14:10

Nick Sabbe