Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Art of R Programming : Where else could I find the information?

Tags:

r

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

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.

I searched in the R manual and found this

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.

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) ?

Thank you for sharing your suggestions

like image 827
harshsinghal Avatar asked Oct 13 '11 13:10

harshsinghal


People also ask

What is R programming used for in data analysis?

R analytics is data analytics using R programming language, an open-source language used for statistical computing or graphics. This programming language is often used in statistical analysis and data mining. It can be used for analytics to identify patterns and build practical models.

What is R programming based on?

The R language was closely modeled on the S Language for Statistical Computing conceived by John Chambers, Rick Becker, Trevor Hastie, Allan Wilks and others at Bell Labs in the mid 1970s, and made publicly available in the early 1980's. Robert and Ross established R as an open source project in 1995.


2 Answers

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.

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.

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.

like image 152
G. Grothendieck Avatar answered Oct 31 '22 16:10

G. Grothendieck


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.

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.

like image 27
NPE Avatar answered Oct 31 '22 17:10

NPE