Skip to content

Instantly share code, notes, and snippets.

@thomaskelder
Created April 9, 2013 06:17
Show Gist options
  • Select an option

  • Save thomaskelder/5343353 to your computer and use it in GitHub Desktop.

Select an option

Save thomaskelder/5343353 to your computer and use it in GitHub Desktop.
#############################
#### Apply functions ####
#############################
matrixData = matrix(1:40, nrow = 10, ncol = 4)
### Median of rows
## Using for loop
rowm = rep(NA, nrow(matrixData))
for(r in 1:nrow(matrixData)) {
rowm[r] = median(matrixData[r,])
}
rowm
## Using apply
apply(matrixData, 1, median)
## Apply on columns instead
apply(matrixData, 2, median)
## Apply on lists
listData = list(a = rnorm(10), b = rnorm(10), c = rnorm(10))
lapply(listData, mean)
lapply(listData, function(x) x * 100)
## Try not to return a list, but a vector/matrix
sapply(listData, mean)
sapply(listData, function(x) x * 100)
## Apply on multiple lists
listData2 = list(d = 1:10, e = 11:20, f = 21:30)
formatText = function(x, y) {
paste(round(x, 2), y, sep=", ")
}
mapply(formatText, listData, listData2)
#############################
#### Parallell execution ####
#############################
bigdata = lapply(1:10^4, function(x) rnorm(10))
### Multicore ###
library(multicore)
options(mc.cores = 10) ## Set number of cores through options
## Do something non-parallel
system.time({
bigresult = lapply(bigdata, median)
})
system.time({
bigresult = mclapply(bigdata, median)
})
### Foreach ###
library(foreach)
## No parallel execution
system.time({
bigresult = foreach(d = bigdata) %do% median(d)
})
## Parallel execution
## Multiple cores
library(doMC) ## Load parallel backend
registerDoMC(cores = 10)
system.time({
bigresult = foreach(d = bigdata) %dopar% median(d)
})
## Other backends available in do* packages (e.g. doSNOW for SNOW clusters)
## Different .combine function
bigmatrix = foreach(d = bigdata, .combine = cbind) %dopar% d / 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment