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.
Thursday, July 12, 2007
Subscribe to:
Post Comments (Atom)
4 comments:
Exceptional post however I was wondering if you
could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit further. Thank you!
Feel free to visit my weblog; lainaa ilman korkoa ja kuluja
An outstanding share! I have just forwarded this onto a
co-worker who was conducting a little research on this.
And he in fact ordered me lunch because I discovered it for him.
.. lol. So allow me to reword this.... Thank YOU for the meal!
! But yeah, thanx for spending the time to discuss this matter here on
your internet site.
my page - registry cleaner software
Thanks for this post. It has helped me to understand sapply and lapply in R
Thanks for More information visit
Packers and Movers Hyderabad
Packers and Movers Bangalore
Packers and Movers Pune
Packers and Movers Mumbai
Post a Comment