Get startedGet started for free

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.

This exercise is part of the course

Parallel Programming in R

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Make a function out of existing code
add_range <- function (file) {
  ___
  
  
  
  
  
}
# Make FORK cluster of four cores
cluster <- makeCluster(___, type = "___")
# Apply add_range to stock_list in parallel
out <- parLapply(___, ___, ___)
# Stop cluster
___
Edit and Run Code