Writing Efficient R Code
Colin Gillespie
Jumping Rivers & Newcastle University
library("parallel")
apply()
is similar to a for loop
A 10 column, 10,000 row matrix:
m <- matrix(rnorm(100000), ncol = 10)
res <- apply(m, 1, median)
parApply()
library("parallel")
copies_of_r <- 7
cl <- makeCluster(copies_of_r)
parApply(cl, m, 1, median)
stopCluster(cl)
As Lewis Caroll said
The hurrier I go, the behinder I get.
# Serial version
apply(m, 1, median)
# Parallel version
parApply(cl, m, 1, median)
Writing Efficient R Code