BaşlayınÜcretsiz Başlayın

foreach with variables and packages

The rate at which a population grows is called the growth rate, expressed as a percentage. Almost every country is experiencing a drop in growth rate, but some are declining faster than others.

You were hired as a Demographics Analyst, and your task is to calculate the the ratio between a country's population growth rate and the global growth rate for the years 2017 to 2021. You have been given a list of data frames, ls_df, each is a data frame like this:

    country year growth_rate
Afghanistan 2017        2.52
...

You also have a global growth data frame, df_global:

year global_growth
2017          1.15
...

foreach and dplyr have been loaded for you. The cluster has already been created and registered with the foreach backend.

Bu egzersiz

Parallel Programming in R

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Export df_global to the cluster within the foreach() function call.
  • Specify the dplyr package required for the calculation.
  • Specify rbind as the combine method.
  • Use the parallel operator.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

df_comp <- foreach(df = ls_df,
                   # Export variable
                   ___,
                   # Specify any package needed
                   ___,
                   # Specify a combine method 
                   ___
                   # Use the parallel operator
                  ) ___ {
    df %>%
      left_join(df_global, "year") %>% 
      mutate(comp = growth_rate/global_growth)
}

stopCluster(cl)
Kodu Düzenle ve Çalıştır