Below are the solutions to these exercises on parallel computing with `foreach`

and `doParallel`

.

#################### # # # Exercise 1 # # # #################### require(foreach) result <- foreach(i = 1:3) %do% sqrt(i) print(result)

## [[1]] ## [1] 1 ## ## [[2]] ## [1] 1.414214 ## ## [[3]] ## [1] 1.732051

class(result)

## [1] "list"

original <- foreach(i = result) %do% i^2 print(original)

## [[1]] ## [1] 1 ## ## [[2]] ## [1] 2 ## ## [[3]] ## [1] 3

#################### # # # Exercise 2 # # # #################### require(foreach) result <- foreach(a = 1:3, b = rep(10, 5)) %do% (a + b) print(result)

## [[1]] ## [1] 11 ## ## [[2]] ## [1] 12 ## ## [[3]] ## [1] 13

length(result)

## [1] 3

# the expression (a + b) can be replaced with sum(a, b) # the length of resulting list equals to the length of the shortest iteration # variable; the remaining elements of the longer variable(s) are ignored #################### # # # Exercise 3 # # # #################### require(foreach) require(iterators) set.seed(1234) result <- foreach(v = irnorm(5, count = 3)) %do% max(v) print(result)

## [[1]] ## [1] 1.084441 ## ## [[2]] ## [1] 0.5060559 ## ## [[3]] ## [1] 0.9594941

#################### # # # Exercise 4 # # # #################### require(foreach) require(iterators) # with .combine = 'c' result_c <- foreach(v = irnorm(5, count = 3), .combine = 'c') %do% v print(result_c)

## [1] -0.1102855 -0.5110095 -0.9111954 -0.8371717 2.4158352 0.1340882 ## [7] -0.4906859 -0.4405479 0.4595894 -0.6937202 -1.4482049 0.5747557 ## [13] -1.0236557 -0.0151383 -0.9359486

class(result_c)

## [1] "numeric"

length(result_c)

## [1] 15

# with .combine = 'cbind' result_cbind <- foreach(v = irnorm(5, count = 3), .combine = 'cbind') %do% v print(result_cbind)

## result.1 result.2 result.3 ## [1,] 1.1022975 -1.1676193 1.4494963 ## [2,] -0.4755931 -2.1800396 -1.0686427 ## [3,] -0.7094400 -1.3409932 -0.8553646 ## [4,] -0.5012581 -0.2942939 -0.2806230 ## [5,] -1.6290935 -0.4658975 -0.9943401

class(result_cbind)

## [1] "matrix"

dim(result_cbind)

## [1] 5 3

#################### # # # Exercise 5 # # # #################### require(foreach) require(iterators) set.seed(1234) result_sum <- foreach(v = irnorm(5, count = 3), .combine = '+') %do% v print(result_sum)

## [1] -1.1782026 -1.2956972 -0.2384446 -2.8456909 0.4985809

#################### # # # Exercise 6 # # # #################### require(foreach) result <- foreach(i = 1:10, .combine = 'c') %:% when(i%%2 == 0) %do% log(i) print(result)

## [1] 0.6931472 1.3862944 1.7917595 2.0794415 2.3025851

#################### # # # Exercise 7 # # # #################### get_column_means_from_file <- function(i) { file_name <- paste0("test_data_", i, ".csv") data <- read.csv(file_name) column_means <- colMeans(data) return(column_means) } #################### # # # Exercise 8 # # # #################### require(parallel) require(doParallel) cluster <- makeCluster(4) registerDoParallel(cluster) #################### # # # Exercise 9 # # # #################### require(foreach) require(parallel) # perform the task sequentially system.time( result_do <- foreach(i = 0:9, .combine = "rbind") %do% get_column_means_from_file(i) )

## user system elapsed ## 2.515 0.000 2.516

# perform the task in parallel system.time( result_dopar <- foreach(i = 0:9, .combine = "rbind") %dopar% get_column_means_from_file(i) )

## user system elapsed ## 0.016 0.002 1.215

# stop the cluster stopCluster(cluster) # print the result print(result_dopar)

## X1 X2 X3 X4 ## result.1 2.264087 3.954797 4.781598 -5.475593 ## result.2 4.515682 3.804385 4.228662 -2.306505 ## result.3 1.992784 2.436252 3.825884 -5.287083 ## result.4 1.888663 1.080326 4.219806 -5.746026 ## result.5 5.036200 5.740396 2.248386 -5.120875 ## result.6 2.970143 1.363111 4.450960 -2.582790 ## result.7 1.214547 3.029435 1.303175 -1.177478 ## result.8 3.537188 4.107851 2.801648 -1.113239 ## result.9 5.946779 1.181247 2.627857 -4.287229 ## result.10 4.783360 3.476798 5.275350 -1.456650

#################### # # # Exercise 10 # # # #################### require(parallel) require(doParallel) require(foreach) # modify the function get_mean_variance_from_first_column <- function(i) { file_name <- paste0("test_data_", i, ".csv") data <- read.csv(file_name) result <- c(mean(data[, 1]), var(data[, 1])) return(result) } # make and register a cluster cluster <- makeCluster(4) registerDoParallel(cluster) # run the function in parallel to get a matrix result <- foreach(i = 0:9, .combine = "rbind") %dopar% get_mean_variance_from_first_column(i) # print the result print(result)

## [,1] [,2] ## result.1 2.264087 5.5159722 ## result.2 4.515682 0.4590161 ## result.3 1.992784 4.4666046 ## result.4 1.888663 9.5151482 ## result.5 5.036200 2.2816551 ## result.6 2.970143 3.1028935 ## result.7 1.214547 3.6991490 ## result.8 3.537188 4.0098893 ## result.9 5.946779 3.0195414 ## result.10 4.783360 3.7131472

# stop the cluster stopCluster(cluster)

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

## Leave a Reply