Get startedGet started for free

Debugging correlations

You work for the US Department of Health. Your team is investigating whether having multiple pregnancies (twins, triplets, etc.) is correlated with more weight gain during pregnancy. You calculated the correlation but your values differ significantly from the estimates of another analyst. She has pointed out that your data does not record values for weight gain greater than 99 pounds. You have decided to run the calculation again, and this time you want to log the maximum value for weight gained.

In your workspace, you have a list of data frames, ls_df. Each element of ls_df contains birth data for each state. You have written a foreach loop to run the calculation in parallel.

foreach and doParallel have been loaded for you.

This exercise is part of the course

Parallel Programming in R

View Course

Exercise instructions

  • Create a cluster of four cores.
  • Specify a file called state_log.txt where messages from the cluster will be logged.
  • Within the loop body, log the maximum value of column weight_gain_pounds in data frame df.
  • Read state_log.txt to check print messages.

Hands-on interactive exercise

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

# Create a cluster of four cores
cl <- ___(___,
# Specify a file to log print statements                    
          ___ = "___")
registerDoParallel(cl)
res <- foreach(df = ls_df) %dopar% {
   print(paste(unique(df$state), ":",
# Calculate maximum of weight_gain_pounds in df
               ___(___)))
                 
  cor(df$plurality, df$weight_gain_pounds)
}
stopCluster(cl)
# Read log file to check print messages
read.delim("___", sep = ";")
Edit and Run Code