Get startedGet started for free

Healthcare spending with clusterMap()

In a recent healthcare conference, it was claimed that total healthcare expenditure is increasing globally.

You were in attendance, and would like to get some rough estimates to test this claim. You have sourced a dataset that gives you per capita healthcare spending and the population for the year 2000 and onwards. You have two lists: ls_pop and ls_exp. They contain population sizes and per capita healthcare spending, respectively, for each country.

The function total_exp() that takes two arguments: pop or population and exp_pc, per capital healthcare spending. This function returns the total spending in billions of US dollars. You would like to apply this function to ls_pop and ls_exp in parallel. The parallel package has been loaded.

This exercise is part of the course

Parallel Programming in R

View Course

Exercise instructions

  • Apply total_exp() in parallel to each element of ls_pop and ls_exp.
  • Specify the list whose elements will be supplied to the pop argument of total_exp().
  • Similarly, specify the list to be supplied to the exp_pc argument of total_exp().

Hands-on interactive exercise

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

total_exp <- function (pop, exp_pc) {
 sum(pop * exp_pc)/1e9
}

cl <- makeCluster(4)
# Apply total_exp using cl to multiple arguments
ls_total <- ___(___, ___,
                       # Specify first argument
                       pop = ___,
                       # Specify second argument
                       exp_pc = ___)

stopCluster(cl)

print(paste("Average yearly increase in USD billions:", round(mean(diff(unlist(ls_total))))))
Edit and Run Code