1. Learn
  2. /
  3. Courses
  4. /
  5. Parallel Programming in R

Exercise

Daily price ranges

The daily range of a stock's price is a good marker of it's market volatility.

You work for an investment firm as a Data Analyst. Your boss wants to calculate the daily range for the stocks of the top ten tech companies in the world. You have a list of CSV files, stock_list for the daily stock prices. For each file, you would like to add a range column. The junior Data Analyst has written the following R code to do this:

for (file in file_list) {

  df <- read.csv(file)
  df$range <- 0
  for (r in 1:nrow(df)) {
    df$range[r] <- df$High[r] - df$Low[r]
  }
  write.csv(df, file)
}

You want to run this code in parallel on a dedicated Apple Mac and would like to use a FORK cluster for better results. The parallel package is loaded in your workspace.

Instructions

100 XP
  • Use the code provided to make a function add_range() that will take a file path as argument and add a range column to this file.
  • Make a FORK cluster of four cores.
  • Apply add_range() to every element of stock_list in parallel.
  • Stop the cluster.