Viết mã R hiệu quả
Colin Gillespie
Jumping Rivers & Newcastle University
library("parallel")
apply() giống như một vòng lặp for
Một ma trận 10 cột, 10.000 hàng:
m <- matrix(rnorm(100000), ncol = 10)
apply gọn gàng hơn vòng lặp forres <- apply(m, 1, median)
parApply()library("parallel")
copies_of_r <- 7
cl <- makeCluster(copies_of_r)
parApply(cl, m, 1, median)
stopCluster(cl)
Như Lewis Caroll nói
The hurrier I go, the behinder I get.
# Bản tuần tự
apply(m, 1, median)
# Bản song song
parApply(cl, m, 1, median)
Viết mã R hiệu quả