Before you start
Here we will learn how to program repetitive operations effectively and fast. We start from the basics of a loop for those who are not familiar with the concept. We then cover parallel computation using the future.lapply and parallel package. Those who are familiar with lapply() can go straight to Chapter A.2.
Here are the specific learning objectives of this chapter.
- Learn how to use for loop and
lapply()to complete repetitive jobs - Learn how not to loop things that can be easily vectorized
- Learn how to parallelize repetitive jobs using the
future_lapply()function from thefuture.applypackage
Packages to install and load
Run the following code to install or load (if already installed) the pacman package, and then install or load (if already installed) the listed package inside the pacman::p_load() function.
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
dplyr, # data wrangling
data.table # data wrangling
) There are other packages that will be loaded during the demonstration.