Thursday, July 12, 2007

Using lapply in R

R is generally pretty good, although it has more quirks than other languages, such as automatically dropping dimensions from an array. (You have to specify drop=false every time, there is no way to change the default, apparently the R development team is taking cues from Microsoft.)

Anyway, buried in the documentation is the lapply function, which makes your code run faster when it applies. It can help you avoid having to link in compiled code, i.e., Fortran or C. Here is an example of it in use.

y <- function(x) { z <- x^3 }

x <- 3:5
b <- lapply(x, y)

w <- 1:6
z <- 4
b <- lapply(w, function(x) {
g <- x^3+z
h <- g*2
})

The first example applies the function y to each element in x. The second example applies the values in w to function(x). It shows that you can access global variables inside the function used inside lapply.