4.2 Arrange

Let’s start with arrange(). Arrange simply arranges the data in a particular order. Let’s order the SDG data by Gini coefficient. Note that the data is by default arranged from lowest to highest. We can alter this by specifying desc(), which will put the data in descending order.

We’ve used two pipes here – first we took the sdg dataset and piped it into the arrange function (and thus sorting the dataset by Gini coefficient). We then took that sorted dataset and piped it into head() to view the first six rows. In this way, chaining pipes is like building up a sentence. We would have to use a number of nested brackets to do this otherwise, which is much harder to follow.

#--- Arrange by Gini
sdg %>% arrange(gini) %>% head()
##           country code reg       gdp gini      pop delta.pop int.migrant
## 1         Ukraine  UKR EUR  2185.728 25.5 45004645     -3.81       10.79
## 2         Iceland  ISL EUR 59976.940 25.6   334252     10.03       11.39
## 3        Slovenia  SVN EUR 21304.570 25.7  2064845      2.89       11.41
## 4  Czech Republic  CZE EUR 18266.550 25.9 10561633      3.15        3.84
## 5 Slovak Republic  SVK EUR 16495.990 26.1  5428704      1.04        3.27
## 6      Kazakhstan  KAZ EUR  7510.077 26.5 17797032     16.26       20.12
##     urb delta.urb emp.ratio slums pop.density largest.city sanitation
## 1 69.92      1.95     53.89    NA       77.69         9.43       97.4
## 2 94.23      1.07     71.13    NA        3.33           NA       98.7
## 3 49.63     -0.78     52.26    NA      102.52           NA       99.1
## 4 72.98     -0.55     57.00    NA      136.79        17.18       99.1
## 5 53.47     -1.94     53.49    NA      112.90        13.72       99.4
## 6 53.23     -1.29     67.71    NA        6.59        16.20       97.0
##   water million   tb urb.pov electric pollution urban.pov.hc  primary
## 1  95.5   12.03 91.0    31.2      100 18.878440           NA 96.17551
## 2 100.0      NA  2.4      NA      100  7.789033           NA 99.05151
## 3  99.7      NA  7.2    22.1      100 20.349960           NA 97.73995
## 4 100.0   12.54  5.2    18.9      100 21.408990           NA       NA
## 5 100.0      NA  6.5      NA      100 20.523670           NA       NA
## 6  99.4    8.62 89.0      NA      100 19.670000          1.3 87.38210
##   health.exp tb.cure case.d diarrhea.trt imm.dpt mat.mort nurse.mw
## 1   46.21547      72     74         69.3      19     12.5    6.677
## 2   17.47512      89     87           NA      91       NA   16.353
## 3   12.06582      77     87           NA      94       NA    8.611
## 4   14.32592      76     93           NA      96      5.5    8.326
## 5   22.53609      88     87           NA      96      3.6    6.078
## 6   45.13577      90     89         54.0      82     12.6    7.991
##       beds  ari                lmic
## 1 9.000000 92.3 Lower middle income
## 2 3.200000   NA         High income
## 3 4.552046   NA         High income
## 4 6.800000   NA         High income
## 5 6.000000   NA         High income
## 6 7.200000 81.2 Upper middle income
#--- Same code, no pipe
head(arrange(sdg, gini))
##           country code reg       gdp gini      pop delta.pop int.migrant
## 1         Ukraine  UKR EUR  2185.728 25.5 45004645     -3.81       10.79
## 2         Iceland  ISL EUR 59976.940 25.6   334252     10.03       11.39
## 3        Slovenia  SVN EUR 21304.570 25.7  2064845      2.89       11.41
## 4  Czech Republic  CZE EUR 18266.550 25.9 10561633      3.15        3.84
## 5 Slovak Republic  SVK EUR 16495.990 26.1  5428704      1.04        3.27
## 6      Kazakhstan  KAZ EUR  7510.077 26.5 17797032     16.26       20.12
##     urb delta.urb emp.ratio slums pop.density largest.city sanitation
## 1 69.92      1.95     53.89    NA       77.69         9.43       97.4
## 2 94.23      1.07     71.13    NA        3.33           NA       98.7
## 3 49.63     -0.78     52.26    NA      102.52           NA       99.1
## 4 72.98     -0.55     57.00    NA      136.79        17.18       99.1
## 5 53.47     -1.94     53.49    NA      112.90        13.72       99.4
## 6 53.23     -1.29     67.71    NA        6.59        16.20       97.0
##   water million   tb urb.pov electric pollution urban.pov.hc  primary
## 1  95.5   12.03 91.0    31.2      100 18.878440           NA 96.17551
## 2 100.0      NA  2.4      NA      100  7.789033           NA 99.05151
## 3  99.7      NA  7.2    22.1      100 20.349960           NA 97.73995
## 4 100.0   12.54  5.2    18.9      100 21.408990           NA       NA
## 5 100.0      NA  6.5      NA      100 20.523670           NA       NA
## 6  99.4    8.62 89.0      NA      100 19.670000          1.3 87.38210
##   health.exp tb.cure case.d diarrhea.trt imm.dpt mat.mort nurse.mw
## 1   46.21547      72     74         69.3      19     12.5    6.677
## 2   17.47512      89     87           NA      91       NA   16.353
## 3   12.06582      77     87           NA      94       NA    8.611
## 4   14.32592      76     93           NA      96      5.5    8.326
## 5   22.53609      88     87           NA      96      3.6    6.078
## 6   45.13577      90     89         54.0      82     12.6    7.991
##       beds  ari                lmic
## 1 9.000000 92.3 Lower middle income
## 2 3.200000   NA         High income
## 3 4.552046   NA         High income
## 4 6.800000   NA         High income
## 5 6.000000   NA         High income
## 6 7.200000 81.2 Upper middle income
#--- Arrange by descending Gini
sdg %>% arrange(desc(gini)) %>% head()
##        country code reg        gdp gini       pop delta.pop int.migrant
## 1 South Africa  ZAF AFR  5273.5940 63.4  55908865     15.91        5.77
## 2       Zambia  ZMB AFR  1178.3880 57.1  16591390     33.98        0.79
## 3       Brazil  BRA AMR  8649.9480 51.3 208000000      9.86        0.34
## 4     Colombia  COL AMR  5805.6050 51.1  48653419     10.99        0.28
## 5       Panama  PAN AMR 13680.2400 51.0   4034119     18.93        4.70
## 6       Rwanda  RWA AFR   702.8356 50.4  11917508     29.45        3.80
##     urb delta.urb emp.ratio slums pop.density largest.city sanitation
## 1 65.30      5.22     39.47  23.0       46.09        26.34       69.6
## 2 41.38      4.35     69.65  54.0       22.32        33.29       55.6
## 3 85.93      2.79     59.29  22.3       24.84        11.93       88.0
## 4 76.71      2.83     61.83  13.1       43.85        26.71       85.2
## 5 66.90      2.94     61.60  25.8       54.27        63.30       83.5
## 6 29.78      9.61     82.73  53.2      483.08        36.45       58.5
##   water million  tb urb.pov  electric pollution urban.pov.hc  primary
## 1  99.6   37.03 834      NA  94.06103  29.62863           NA       NA
## 2  85.6   13.77 391     7.7  61.50000  26.74665           NA 87.40411
## 3 100.0   40.05  41      NA  99.94555  11.36829           NA 92.69713
## 4  96.8   43.12  31      NA  99.83837  17.99814         24.1 90.60457
## 5  97.7   42.34  50      NA 100.00000  13.19621           NA 93.40181
## 6  86.6   10.85  56      NA  71.80000  49.71362         22.1 95.09565
##   health.exp tb.cure case.d diarrhea.trt imm.dpt mat.mort nurse.mw beds
## 1   6.491224      78     63           NA      66   200.00    5.113   NA
## 2  29.994880      85     58         56.2      91   398.00    0.714  2.0
## 3  25.469930      71     87           NA      86    58.20    7.444  2.3
## 4  15.355020      76     80         52.0      91    71.22    0.656  1.5
## 5  22.267520      79     80         52.7      73    80.50    2.330  2.2
## 6  28.130080      86     84         19.5      98   253.00    0.678   NA
##    ari                lmic
## 1   NA Upper middle income
## 2 69.7 Lower middle income
## 3   NA Upper middle income
## 4 65.3 Upper middle income
## 5 81.6 Upper middle income
## 6 53.9          Low income

EXERCISE: Which country has the highest GDP? Which has the lowest TB cure rate?