The thing that makes R different from commercial packages like SAS or Excel is that it's open-source. This means that if you ever wonder exactly how R calculates a certain value, or you want to adapt an existing R routine for your own purposes, you can always find the source code R uses and take a look yourself.
Much of R is written in the R programming language itself: if you want to see how a function is R is defined, all it usually takes is to type the name of the function at the command line. For example, to see how the sd (standard deviation) function in R works, just type its name:
> sd
function (x, na.rm = FALSE)
{
if (is.matrix(x))
apply(x, 2, sd, na.rm = na.rm)
else if (is.vector(x))
sqrt(var(x, na.rm = na.rm))
else if (is.data.frame(x))
sapply(x, sd, na.rm = na.rm)
else sqrt(var(as.vector(x), na.rm = na.rm))
}
This works because in R, functions are just like any variable like a matrix or vector, in the sense that they have a value. (In the usual parlance, functions are "first-class objects" in R.) The value displayed is the definition of the function, in the R language.
There are some wrinkles in finding the source code for some functions: some are in other packages or namespaces, and some are implemented in the core R sources in C or Fortran. You can find detailed instructions on finding the source code in these cases in a 2006 R-News article by Uwe Ligges:
Accessing the Sources (see pages 44-45). But in general, the source is always there for you to look at if you need it.
While I wholeheartedly agree with the concept, the example is not very good. The only thing we learn from that function call is that if we use sd R calls a function (written in c or fortran) called sd, so we are back at the starting point.
Posted by: Luis | February 03, 2009 at 13:25
Well, no, it mostly calls 'var' -- which is where you hit the .Internal call. If it hits a matrix or a data frame, it calls itself in a way that then calls 'var'.
Still true, mind you, that 'sd' calls 'var' which makes an .Internal call. I think these are mostly coded in C, but I really neither know nor want to know.
Posted by: wcw | February 03, 2009 at 20:34