gpt4 book ai didi

r - R : writing a "function" without defining a function?中的function(x)

转载 作者:行者123 更新时间:2023-12-04 05:30:28 25 4
gpt4 key购买 nike

我已经遇到过几次这个概念,但是不知道它的名字,所以不能用谷歌搜索它以了解更多。基本上,当浏览功能甚至其他人编写的简单命令时,我经常会看到类似的内容:

apply(dataset, 1:2, function(x) 10 * x)

在这种情况下,我能够弄清楚,这个“伪函数” 函数(x)只会将数据集的每个元素乘以10。这似乎是一个有用的功能,但是我仍然不确定何时或您如何使用它。它真的是一个功能吗?还是只在应用功能家族中起作用?这个东西有名字吗?

最佳答案

这些被称为“匿名函数”,是的,它们是实函数对象,在使用前恰好没有被分配给任何符号。

这是R language documentation的相关内容:

Generally functions are assigned to symbols but they don’t need to be. The value returned by the call to function is a function. If this is not given a name it is referred to as an anonymous function. Anonymous functions are most frequently used as arguments to other functions such as the apply family or outer.



尽管它们最常用在 *apply()函数中,但是例如在此处可以不必使用它们
(function(x,y){x*y + x/y})(2,5)
# [1] 10.4

关于r - R : writing a "function" without defining a function?中的function(x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com