R编程的艺术:我还能在哪里找到这些信息?

时间:2022-07-02 15:08:28

I came across the editorial review of the book The Art of R Programming, and found this

我看到了“编程的艺术”一书的编辑评论,并发现了这一点

The Art of R Programming takes you on a guided tour of software development with R, from basic types and data structures to advanced topics like closures, recursion, and anonymous functions

R编程的艺术带您了解R的软件开发,从基本类型和数据结构到高级主题,如闭包,递归和匿名函数

I immediately became fascinated by the idea of anonymous functions, something I had come across in Python in the form of lambda functions but could not make the connection in the R language.

我立即对匿名函数的想法着了迷,这是我在Python中以lambda函数的形式遇到的但是无法在R语言中建立连接。

I searched in the R manual and found this

我在R手册中搜索并找到了这个

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 other functions such as the apply family or outer.

通常将函数分配给符号,但它们不需要。调用函数返回的值是一个函数。如果没有给出名称,则将其称为匿名函数。匿名函数最常用作其他函数的参数,例如apply family或outer。

These things for a not-very-long-time programmer like me are "quirky" in a very interesting sort of way. Where can I find more of these for R (without having to buy a book) ?

对于像我这样不太长时间的程序员来说,这些东西是以一种非常有趣的方式“古怪”。哪里可以找到更多这些R(无需购买书籍)?

Thank you for sharing your suggestions

感谢您分享您的建议

4 个解决方案

#1


17  

Functions don't have names in R. Whether you happen to put a function into a variable or not is not a property of the function itself so there does not exist two sorts of functions: anonymous and named. The best we can do is to agree to call a function which has never been assigned to a variable anonymous.

函数在R中没有名称。您是否碰巧将函数放入变量中并不是函数本身的属性,因此不存在两种函数:匿名函数和命名函数。我们能做的最好的事情是同意调用一个从未被赋予匿名变量的函数。

A function f can be regarded as a triple consisting of its formal arguments, its body and its environment accessible individually via formals(f), body(f) and environment(f). The name is not any part of that triple. See the function objects part of the language definition manual.

函数f可以被视为由其形式论证组成的三元组,其正文和环境可通过形式(f),正文(f)和环境(f)单独访问。这个名字不是那个三重奏的任何一部分。请参阅语言定义手册中的函数对象部分。

Note that if we want a function to call itself then we can use Recall to avoid knowing whether or not the function was assigned to a variable. The alternative is that the function body must know that the function has been assigned to a particular variable and what the name of that variable is. That is, if the function is assigned to variable f, say, then the body can refer to f in order to call itself. Recall is limited to self-calling functions. If we have two functions which mutually call each other then a counterpart to Recall does not exist -- each function must name the other which means that each function must have been assigned to a variable and each function body must know the variable name that the other function was assigned to.

请注意,如果我们想要一个函数来调用自身,那么我们可以使用Recall来避免知道函数是否被赋值给变量。另一种方法是函数体必须知道函数已分配给特定变量以及该变量的名称是什么。也就是说,如果将函数赋值给变量f,那么主体可以引用f来调用自身。召回仅限于自我调用功能。如果我们有两个相互调用的函数,那么Recall的对应物就不存在 - 每个函数必须命名另一个函数,这意味着每个函数必须已经分配给一个变量,每个函数体必须知道另一个的变量名。功能被分配给。

#2


5  

There's not a lot to say about anonymous functions in R. Unlike Python, where lambda functions require special syntax, in R an anonymous function is simply a function without a name.

关于R中的匿名函数没有太多可说的。与Python不同,lambda函数需要特殊语法,在R中,匿名函数只是一个没有名称的函数。

For example:

例如:

function(x,y) { x+y }

whereas a normal, named, function would be

而正常的,命名的功能将是

add <- function(x,y) { x+y }

Functions are first-class objects, so you can pass them (regardless of whether they're anonymous) as arguments to other functions. Examples of functions that take other functions as arguments include apply, lapply and sapply.

函数是第一类对象,因此您可以将它们(无论它们是否是匿名的)作为其他函数的参数传递。将其他函数作为参数的函数示例包括apply,lapply和sapply。

#3


4  

Get Patrick Burns' "The R Inferno" at his site

在他的网站上获得Patrick Burns的“The R Inferno”

There are several good web sites with basic introductions to R usage.

有几个很好的网站,基本介绍了R的用法。

I also like Zoonekynd's manual

我也喜欢Zoonekynd的手册

#4


2  

Great answers about style so far. Here's an answer about a typical use of anonymous functions in R:

到目前为止关于风格的好答案。以下是关于R中匿名函数的典型使用的答案:

# Make some data up
my.list <- list()
for( i in seq(100) ) {
   my.list[[i]] <- lm( runif(10) ~ runif(10) )
}

# Do something with the data
sapply( my.list, function(x) x$qr$rank )

We could have named the function, but for simple data extractions and so forth it's really handy not to have to.

我们可以命名这个函数,但是对于简单的数据提取等等,它真的很方便。

#1


17  

Functions don't have names in R. Whether you happen to put a function into a variable or not is not a property of the function itself so there does not exist two sorts of functions: anonymous and named. The best we can do is to agree to call a function which has never been assigned to a variable anonymous.

函数在R中没有名称。您是否碰巧将函数放入变量中并不是函数本身的属性,因此不存在两种函数:匿名函数和命名函数。我们能做的最好的事情是同意调用一个从未被赋予匿名变量的函数。

A function f can be regarded as a triple consisting of its formal arguments, its body and its environment accessible individually via formals(f), body(f) and environment(f). The name is not any part of that triple. See the function objects part of the language definition manual.

函数f可以被视为由其形式论证组成的三元组,其正文和环境可通过形式(f),正文(f)和环境(f)单独访问。这个名字不是那个三重奏的任何一部分。请参阅语言定义手册中的函数对象部分。

Note that if we want a function to call itself then we can use Recall to avoid knowing whether or not the function was assigned to a variable. The alternative is that the function body must know that the function has been assigned to a particular variable and what the name of that variable is. That is, if the function is assigned to variable f, say, then the body can refer to f in order to call itself. Recall is limited to self-calling functions. If we have two functions which mutually call each other then a counterpart to Recall does not exist -- each function must name the other which means that each function must have been assigned to a variable and each function body must know the variable name that the other function was assigned to.

请注意,如果我们想要一个函数来调用自身,那么我们可以使用Recall来避免知道函数是否被赋值给变量。另一种方法是函数体必须知道函数已分配给特定变量以及该变量的名称是什么。也就是说,如果将函数赋值给变量f,那么主体可以引用f来调用自身。召回仅限于自我调用功能。如果我们有两个相互调用的函数,那么Recall的对应物就不存在 - 每个函数必须命名另一个函数,这意味着每个函数必须已经分配给一个变量,每个函数体必须知道另一个的变量名。功能被分配给。

#2


5  

There's not a lot to say about anonymous functions in R. Unlike Python, where lambda functions require special syntax, in R an anonymous function is simply a function without a name.

关于R中的匿名函数没有太多可说的。与Python不同,lambda函数需要特殊语法,在R中,匿名函数只是一个没有名称的函数。

For example:

例如:

function(x,y) { x+y }

whereas a normal, named, function would be

而正常的,命名的功能将是

add <- function(x,y) { x+y }

Functions are first-class objects, so you can pass them (regardless of whether they're anonymous) as arguments to other functions. Examples of functions that take other functions as arguments include apply, lapply and sapply.

函数是第一类对象,因此您可以将它们(无论它们是否是匿名的)作为其他函数的参数传递。将其他函数作为参数的函数示例包括apply,lapply和sapply。

#3


4  

Get Patrick Burns' "The R Inferno" at his site

在他的网站上获得Patrick Burns的“The R Inferno”

There are several good web sites with basic introductions to R usage.

有几个很好的网站,基本介绍了R的用法。

I also like Zoonekynd's manual

我也喜欢Zoonekynd的手册

#4


2  

Great answers about style so far. Here's an answer about a typical use of anonymous functions in R:

到目前为止关于风格的好答案。以下是关于R中匿名函数的典型使用的答案:

# Make some data up
my.list <- list()
for( i in seq(100) ) {
   my.list[[i]] <- lm( runif(10) ~ runif(10) )
}

# Do something with the data
sapply( my.list, function(x) x$qr$rank )

We could have named the function, but for simple data extractions and so forth it's really handy not to have to.

我们可以命名这个函数,但是对于简单的数据提取等等,它真的很方便。