R 中类似 lambda 的函数?

发布于 2024-12-11 08:35:09 字数 432 浏览 0 评论 0原文

我需要在大学的新讲座中使用/学习 R,而我目前在其语法方面遇到了一些困难。我想绘制(通过曲线)一个简单的函数,但我似乎无法让它与内联 lambda 类函数一起使用。

我已经尝试过以下操作:

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

但是,当我首先将函数存储在变量中时,它会起作用:

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

R 中不允许这样的内联使用吗?有没有其他方法可以在不定义额外函数的情况下完成这项工作?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深白境迁sunset 2024-12-18 08:35:09

只是为了完整性。您可以在 R 中使用“类似 lambda”(匿名)函数,但如果您想立即使用它们,则需要将函数定义括在括号或大括号中:

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

对于 curve第一个参数是表达式或函数名称 - 但如果它是函数名称,则首先将其转换为表达式。 (请参阅curve源代码中的前几行)。因此,如果它不是函数名称,您将需要一个表达式 - 它可能包含一个“lambda”函数:

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

如果您想使用函数(而不是其名称)作为参数,您可以使用plot.function:

plot(function(x) x^2)

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)
故事↓在人 2024-12-18 08:35:09

R 4.1 开始,您可以使用 \(x) 类似 lambda 的简写:

R 现在提供了用于创建匿名函数的简写符号,
例如 \(x) x + 1 被解析为 function(x) x + 1。

对于 function(x) x^2

(\(x) x^2)(2)
#[1] 4

这可以与 curve 一起使用:

curve((\(x) x^2)(x))

< a href="https://i.sstatic.net/RY0fx.png" rel="noreferrer">在此处输入图像描述

但正如所述在评论中,在这种情况下,表达式更简单:

curve(x^2)

From R 4.1 on, you can use \(x) lambda-like shorthand:

R now provides a shorthand notation for creating anonymous functions,
e.g. \(x) x + 1 is parsed as function(x) x + 1.

With function(x) x^2:

(\(x) x^2)(2)
#[1] 4

This can be used with curve :

curve((\(x) x^2)(x))

enter image description here

But as stated in comments, in this case an expression is more straightforward :

curve(x^2)
℉服软 2024-12-18 08:35:09

您必须查看 curve 的源代码才能了解正在发生的情况(只需在提示符处输入 curve 并按 Enter 键即可)。

在那里您可以找到如何解析传递的表达式。

发现函数本身的唯一方法是仅传递其名称(请参阅 is.name 部分)。如果不是这种情况,则为每个 x 调用该表达式。在你的情况下:对于每个x,结果是一个函数,这对于绘图来说不是一个愉快的想法......

所以简而言之:不,你不能做你尝试过的事情,但正如@ROLO所指出的,您可以立即传递函数体,该函数体将被解析为表达式(并且应包含 x)。如果其中包含多个语句,只需将它们括在花括号中即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文