library(parallel) # Check how many cores are requested by the job ( ncores <- as.numeric( Sys.getenv("NSLOTS") ) ) # If code is executed interactively, set the number of cores manually if ( is.na(ncores) ) ncores <- 1 # Define a function to be executed by each core myFun <- function(x){ sum(x ) } # Define a sample dataset set.seed(42) df <- as.data.frame( matrix(sample(1:100, 100000000, replace=TRUE), nrow=100000) ) # Create a Parallel Socket Cluster from multiple CPU cores cl <- makeCluster( ncores ) # Execute function in parallel res <- clusterApply( cl, x = df, fun = myFun) # Stop the cluster stopCluster(cl)