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.
Cet exercice fait partie du cours
Parallel Programming in R
Instructions
- Apply
total_exp()
in parallel to each element ofls_pop
andls_exp
. - Specify the list whose elements will be supplied to the
pop
argument oftotal_exp()
. - Similarly, specify the list to be supplied to the
exp_pc
argument oftotal_exp()
.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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))))))