Applying functions in R

Installing to tead Excel files:

# install.packages('rio') #installation was not required as it was done in Assignment 1

reading data

# get the link for where the excel file is located
linkGit="https://github.com/megvonguru/HW-2/raw/refs/heads/main/FSI-2023-DOWNLOAD.xlsx"


# library(rio) # package needed
# from the link above, import the file and save it under the name fragility23
fragility23=rio::import(file = linkGit) #object that will hold the result
# display the content of fragility 23 in a readable format 
str(fragility23)
## 'data.frame':    179 obs. of  16 variables:
##  $ Country                         : chr  "Somalia" "Yemen" "South Sudan" "Congo Democratic Republic" ...
##  $ Year                            : num  2023 2023 2023 2023 2023 ...
##  $ Rank                            : chr  "1st" "2nd" "3rd" "4th" ...
##  $ Total                           : num  112 109 109 107 107 ...
##  $ S1: Demographic Pressures       : num  10 9.6 9.7 9.7 7.4 9.2 8.8 9.3 9.5 8.8 ...
##  $ S2: Refugees and IDPs           : num  9 9.6 10 9.8 9.1 8.6 9.6 9.5 9 7.7 ...
##  $ C3: Group Grievance             : num  8.7 8.8 8.6 9.4 9.1 8.3 9.3 8.1 8.1 5.5 ...
##  $ E3: Human Flight and Brain Drain: num  8.6 6.4 6.5 6.4 8 8.5 7.5 6.2 7.7 8.3 ...
##  $ E2: Economic Inequality         : num  9.1 7.9 8.6 8.4 6.5 8.2 8.5 9.6 8.7 9.2 ...
##  $ E1: Economy                     : num  9.5 9.9 8.6 8.1 9.6 9.6 9.3 8.2 8.4 8.9 ...
##  $ P1: State Legitimacy            : num  9.6 9.8 9.8 9.3 10 9.4 9.4 8.9 9.1 9.9 ...
##  $ P2: Public Services             : num  9.8 9.6 9.7 9.3 9 10 8.6 10 9.6 9.8 ...
##  $ P3: Human Rights                : num  9 9.6 8.7 9.3 9.1 8.7 9.2 9.1 8.4 8.7 ...
##  $ C1: Security Apparatus          : num  9.5 8.6 9.9 8.8 9.4 9.7 8.3 8 8.7 6.8 ...
##  $ C2: Factionalized Elites        : num  10 9.9 9.2 9.6 9.9 8.7 9.6 9.4 9.5 9.7 ...
##  $ X1: External Intervention       : num  9.1 9.2 9.2 9.1 10 7.7 8.1 9.4 7.9 9.6 ...

Apply square root function?

# whole DF?
# sqrt(fragility23)
# several columns (only numerical)

# calculate the square root for columns 4 and 5
sqrt(fragility23[,4:5])
##         Total S1: Demographic Pressures
## 1   10.578280                  3.162278
## 2   10.435516                  3.098387
## 3   10.416333                  3.114482
## 4   10.353743                  3.114482
## 5   10.348913                  2.720294
## 6   10.324728                  3.033150
## 7   10.305338                  2.966479
## 8   10.281051                  3.049590
## 9   10.227414                  3.082207
## 10  10.143964                  2.966479
## 11  10.019980                  3.130495
## 12  10.009995                  2.645751
## 13   9.974969                  2.966479
## 14   9.924717                  2.966479
## 15   9.899495                  3.098387
## 16   9.843780                  2.949576
## 17   9.803061                  2.449490
## 18   9.792855                  2.701851
## 19   9.721111                  2.774887
## 20   9.705668                  2.949576
## 21   9.695360                  2.880972
## 22   9.695360                  3.098387
## 23   9.695360                  3.000000
## 24   9.664368                  2.983287
## 25   9.581232                  2.408319
## 26   9.565563                  3.033150
## 27   9.560335                  2.863564
## 28   9.523655                  2.915476
## 29   9.513149                  2.549510
## 30   9.502631                  2.683282
## 31   9.481561                  2.983287
## 32   9.481561                  2.880972
## 33   9.428680                  2.898275
## 34   9.375500                  2.880972
## 35   9.370165                  2.846050
## 36   9.332738                  2.932576
## 37   9.327379                  2.932576
## 38   9.327379                  2.701851
## 39   9.322017                  3.049590
## 40   9.241212                  2.607681
## 41   9.230385                  2.774887
## 42   9.186947                  3.016621
## 43   9.121403                  3.098387
## 44   9.071935                  2.683282
## 45   9.066422                  2.863564
## 46   9.066422                  2.664583
## 47   9.060905                  2.720294
## 48   9.044335                  3.065942
## 49   9.038805                  3.098387
## 50   9.033272                  2.588436
## 51   9.022195                  2.898275
## 52   9.011104                  2.121320
## 53   8.983318                  2.190890
## 54   8.961027                  2.529822
## 55   8.955445                  2.846050
## 56   8.921883                  2.664583
## 57   8.921883                  2.626785
## 58   8.893818                  2.949576
## 59   8.837420                  2.792848
## 60   8.837420                  2.720294
## 61   8.820431                  2.810694
## 62   8.814760                  2.236068
## 63   8.803408                  2.949576
## 64   8.792042                  2.664583
## 65   8.752143                  2.932576
## 66   8.734987                  2.932576
## 67   8.723531                  2.863564
## 68   8.700575                  2.509980
## 69   8.694826                  2.387467
## 70   8.642916                  2.529822
## 71   8.631338                  2.949576
## 72   8.613942                  2.645751
## 73   8.608136                  2.915476
## 74   8.561542                  2.828427
## 75   8.549854                  2.756810
## 76   8.526429                  1.949359
## 77   8.502941                  2.469818
## 78   8.485281                  2.828427
## 79   8.479387                  2.167948
## 80   8.455767                  2.720294
## 81   8.408329                  2.626785
## 82   8.378544                  2.144761
## 83   8.366600                  2.236068
## 84   8.360622                  2.190890
## 85   8.354639                  2.626785
## 86   8.348653                  2.626785
## 87   8.330666                  2.588436
## 88   8.324662                  2.408319
## 89   8.324662                  2.607681
## 90   8.258329                  2.097618
## 91   8.246211                  2.469818
## 92   8.240146                  2.024846
## 93   8.215838                  1.949359
## 94   8.209750                  2.323790
## 95   8.173127                  2.073644
## 96   8.148620                  2.323790
## 97   8.148620                  2.073644
## 98   8.099383                  2.645751
## 99   8.093207                  2.588436
## 100  8.080842                  2.073644
## 101  8.068457                  2.167948
## 102  8.068457                  2.024846
## 103  8.068457                  2.549510
## 104  8.031189                  2.366432
## 105  7.981228                  2.387467
## 106  7.930952                  2.280351
## 107  7.893035                  2.720294
## 108  7.867655                  2.190890
## 109  7.848567                  2.408319
## 110  7.797435                  2.529822
## 111  7.784600                  2.024846
## 112  7.765307                  2.846050
## 113  7.765307                  2.049390
## 114  7.752419                  2.489980
## 115  7.733046                  2.236068
## 116  7.726578                  2.345208
## 117  7.713624                  2.428992
## 118  7.635444                  2.097618
## 119  7.615773                  2.024846
## 120  7.549834                  1.843909
## 121  7.536577                  2.024846
## 122  7.436397                  2.792848
## 123  7.422937                  2.024846
## 124  7.416198                  2.258318
## 125  7.395945                  1.673320
## 126  7.334848                  1.923538
## 127  7.328028                  2.024846
## 128  7.300685                  2.049390
## 129  7.280110                  2.121320
## 130  7.273239                  2.024846
## 131  7.197222                  2.323790
## 132  7.162402                  2.190890
## 133  7.155418                  2.097618
## 134  7.014271                  2.509980
## 135  6.985700                  1.870829
## 136  6.978539                  2.213594
## 137  6.978539                  2.049390
## 138  6.978539                  2.190890
## 139  6.811755                  2.387467
## 140  6.737952                  1.923538
## 141  6.730527                  2.280351
## 142  6.723095                  1.949359
## 143  6.640783                  2.190890
## 144  6.595453                  1.816590
## 145  6.580274                  1.788854
## 146  6.526868                  2.144761
## 147  6.496153                  2.428992
## 148  6.473021                  1.843909
## 149  6.363961                  1.732051
## 150  6.356099                  1.702939
## 151  6.340347                  1.788854
## 152  6.276942                  1.897367
## 153  6.212890                  1.702939
## 154  6.164414                  1.760682
## 155  6.148170                  1.612452
## 156  6.082763                  1.897367
## 157  5.865151                  1.843909
## 158  5.744563                  1.923538
## 159  5.612486                  1.581139
## 160  5.603570                  2.049390
## 161  5.522681                  2.428992
## 162  5.366563                  1.760682
## 163  5.224940                  1.897367
## 164  5.069517                  2.000000
## 165  5.049752                  1.581139
## 166  4.959839                  1.516575
## 167  4.939636                  1.897367
## 168  4.690416                  1.949359
## 169  4.582576                  1.581139
## 170  4.538722                  1.732051
## 171  4.415880                  1.549193
## 172  4.415880                  1.673320
## 173  4.347413                  1.095445
## 174  4.230839                  1.516575
## 175  4.219005                  1.549193
## 176  4.086563                  1.048809
## 177  4.000000                  1.303840
## 178  3.962323                  1.224745
## 179  3.807887                  1.183216
# one column
# calculate the square root of each value in the column total
sqrt(fragility23$Total)
##   [1] 10.578280 10.435516 10.416333 10.353743 10.348913 10.324728 10.305338
##   [8] 10.281051 10.227414 10.143964 10.019980 10.009995  9.974969  9.924717
##  [15]  9.899495  9.843780  9.803061  9.792855  9.721111  9.705668  9.695360
##  [22]  9.695360  9.695360  9.664368  9.581232  9.565563  9.560335  9.523655
##  [29]  9.513149  9.502631  9.481561  9.481561  9.428680  9.375500  9.370165
##  [36]  9.332738  9.327379  9.327379  9.322017  9.241212  9.230385  9.186947
##  [43]  9.121403  9.071935  9.066422  9.066422  9.060905  9.044335  9.038805
##  [50]  9.033272  9.022195  9.011104  8.983318  8.961027  8.955445  8.921883
##  [57]  8.921883  8.893818  8.837420  8.837420  8.820431  8.814760  8.803408
##  [64]  8.792042  8.752143  8.734987  8.723531  8.700575  8.694826  8.642916
##  [71]  8.631338  8.613942  8.608136  8.561542  8.549854  8.526429  8.502941
##  [78]  8.485281  8.479387  8.455767  8.408329  8.378544  8.366600  8.360622
##  [85]  8.354639  8.348653  8.330666  8.324662  8.324662  8.258329  8.246211
##  [92]  8.240146  8.215838  8.209750  8.173127  8.148620  8.148620  8.099383
##  [99]  8.093207  8.080842  8.068457  8.068457  8.068457  8.031189  7.981228
## [106]  7.930952  7.893035  7.867655  7.848567  7.797435  7.784600  7.765307
## [113]  7.765307  7.752419  7.733046  7.726578  7.713624  7.635444  7.615773
## [120]  7.549834  7.536577  7.436397  7.422937  7.416198  7.395945  7.334848
## [127]  7.328028  7.300685  7.280110  7.273239  7.197222  7.162402  7.155418
## [134]  7.014271  6.985700  6.978539  6.978539  6.978539  6.811755  6.737952
## [141]  6.730527  6.723095  6.640783  6.595453  6.580274  6.526868  6.496153
## [148]  6.473021  6.363961  6.356099  6.340347  6.276942  6.212890  6.164414
## [155]  6.148170  6.082763  5.865151  5.744563  5.612486  5.603570  5.522681
## [162]  5.366563  5.224940  5.069517  5.049752  4.959839  4.939636  4.690416
## [169]  4.582576  4.538722  4.415880  4.415880  4.347413  4.230839  4.219005
## [176]  4.086563  4.000000  3.962323  3.807887
# one value
# calculate the square root of the first element of the column total
sqrt(fragility23$Total[1])
## [1] 10.57828

Applying sum():

# sum everything
# calculate the sum of all values for columns 4 and 5
sum(fragility23[,4:5])
## [1] 12850.1
# sum by column
# calculate the sum of the columns 4 and 5 and then print the data
print(apply(fragility23[,4:5],2,sum))
##                     Total S1: Demographic Pressures 
##                   11784.0                    1066.1
#notice
# find the sum of columns 4 and 5 and then identify the type of data that is returned 
typeof(apply(fragility23[,4:5],2,sum))
## [1] "double"

If you do not see list, then it is a vector. ⏫

# sum by row
# calculate the sum of rows 4 and 5 and then print the output
print(apply(fragility23[,4:5],1,sum))
##     1     2     3     4     5     6     7     8     9    10    11    12    13 
## 121.9 118.5 118.2 116.9 114.5 115.8 115.0 115.0 114.1 111.7 110.2 107.2 108.3 
##    14    15    16    17    18    19    20    21    22    23    24    25    26 
## 107.3 107.6 105.6 102.1 103.2 102.2 102.9 102.3 103.6 103.0 102.3  97.6 100.7 
##    27    28    29    30    31    32    33    34    35    36    37    38    39 
##  99.6  99.2  97.0  97.5  98.8  98.2  97.3  96.2  95.9  95.7  95.6  94.3  96.2 
##    40    41    42    43    44    45    46    47    48    49    50    51    52 
##  92.2  92.9  93.5  92.8  89.5  90.4  89.3  89.5  91.2  91.3  88.3  89.8  85.7 
##    53    54    55    56    57    58    59    60    61    62    63    64    65 
##  85.5  86.7  88.3  86.7  86.5  87.8  85.9  85.5  85.7  82.7  86.2  84.4  85.2 
##    66    67    68    69    70    71    72    73    74    75    76    77    78 
##  84.9  84.3  82.0  81.3  81.1  83.2  81.2  82.6  81.3  80.7  76.5  78.4  80.0 
##    79    80    81    82    83    84    85    86    87    88    89    90    91 
##  76.6  78.9  77.6  74.8  75.0  74.7  76.7  76.6  76.1  75.1  76.1  72.6  74.1 
##    92    93    94    95    96    97    98    99   100   101   102   103   104 
##  72.0  71.3  72.8  71.1  71.8  70.7  72.6  72.2  69.6  69.8  69.2  71.6  70.1 
##   105   106   107   108   109   110   111   112   113   114   115   116   117 
##  69.4  68.1  69.7  66.7  67.4  67.2  64.7  68.4  64.5  66.3  64.8  65.2  65.4 
##   118   119   120   121   122   123   124   125   126   127   128   129   130 
##  62.7  62.1  60.4  60.9  63.1  59.2  60.1  57.5  57.5  57.8  57.5  57.5  57.0 
##   131   132   133   134   135   136   137   138   139   140   141   142   143 
##  57.2  56.1  55.6  55.5  52.3  53.6  52.9  53.5  52.1  49.1  50.5  49.0  48.9 
##   144   145   146   147   148   149   150   151   152   153   154   155   156 
##  46.8  46.5  47.2  48.1  45.3  43.5  43.3  43.4  43.0  41.5  41.1  40.4  40.6 
##   157   158   159   160   161   162   163   164   165   166   167   168   169 
##  37.8  36.7  34.0  35.6  36.4  31.9  30.9  29.7  28.0  26.9  28.0  25.8  23.5 
##   170   171   172   173   174   175   176   177   178   179 
##  23.6  21.9  22.3  20.1  20.2  20.2  17.8  17.7  17.2  15.9

Apply by iterating:

# calculate the sum of all values in the 4th and 5th column and then print them as a list
print(lapply(fragility23[,4:5],sum))
## $Total
## [1] 11784
## 
## $`S1: Demographic Pressures`
## [1] 1066.1

Notice output of lapply:

# apply the sum function on columns 4 and 5 and identify the type of internal storage of the returned list
typeof(lapply(fragility23[,4:5],sum))
## [1] "list"
# apply the sum function on columns 4 and 5 and identify the type of high level storage of the returned list
class(lapply(fragility23[,4:5],sum))
## [1] "list"

Notice output of sapply:

# calculate the sum of the elements of the columns in 4 and 5 and print the results in a vector format
print(sapply(fragility23[,4:5],sum))
##                     Total S1: Demographic Pressures 
##                   11784.0                    1066.1
# calculate the sum of the elements of the columns in 4 and 5 and identify the high storage type
class(sapply(fragility23[,4:5],sum))
## [1] "numeric"

Similarly:

# do the square root of the elements of the columns in 4 and 5 and then print the list
print(lapply(fragility23[,4:5],sqrt))
## $Total
##   [1] 10.578280 10.435516 10.416333 10.353743 10.348913 10.324728 10.305338
##   [8] 10.281051 10.227414 10.143964 10.019980 10.009995  9.974969  9.924717
##  [15]  9.899495  9.843780  9.803061  9.792855  9.721111  9.705668  9.695360
##  [22]  9.695360  9.695360  9.664368  9.581232  9.565563  9.560335  9.523655
##  [29]  9.513149  9.502631  9.481561  9.481561  9.428680  9.375500  9.370165
##  [36]  9.332738  9.327379  9.327379  9.322017  9.241212  9.230385  9.186947
##  [43]  9.121403  9.071935  9.066422  9.066422  9.060905  9.044335  9.038805
##  [50]  9.033272  9.022195  9.011104  8.983318  8.961027  8.955445  8.921883
##  [57]  8.921883  8.893818  8.837420  8.837420  8.820431  8.814760  8.803408
##  [64]  8.792042  8.752143  8.734987  8.723531  8.700575  8.694826  8.642916
##  [71]  8.631338  8.613942  8.608136  8.561542  8.549854  8.526429  8.502941
##  [78]  8.485281  8.479387  8.455767  8.408329  8.378544  8.366600  8.360622
##  [85]  8.354639  8.348653  8.330666  8.324662  8.324662  8.258329  8.246211
##  [92]  8.240146  8.215838  8.209750  8.173127  8.148620  8.148620  8.099383
##  [99]  8.093207  8.080842  8.068457  8.068457  8.068457  8.031189  7.981228
## [106]  7.930952  7.893035  7.867655  7.848567  7.797435  7.784600  7.765307
## [113]  7.765307  7.752419  7.733046  7.726578  7.713624  7.635444  7.615773
## [120]  7.549834  7.536577  7.436397  7.422937  7.416198  7.395945  7.334848
## [127]  7.328028  7.300685  7.280110  7.273239  7.197222  7.162402  7.155418
## [134]  7.014271  6.985700  6.978539  6.978539  6.978539  6.811755  6.737952
## [141]  6.730527  6.723095  6.640783  6.595453  6.580274  6.526868  6.496153
## [148]  6.473021  6.363961  6.356099  6.340347  6.276942  6.212890  6.164414
## [155]  6.148170  6.082763  5.865151  5.744563  5.612486  5.603570  5.522681
## [162]  5.366563  5.224940  5.069517  5.049752  4.959839  4.939636  4.690416
## [169]  4.582576  4.538722  4.415880  4.415880  4.347413  4.230839  4.219005
## [176]  4.086563  4.000000  3.962323  3.807887
## 
## $`S1: Demographic Pressures`
##   [1] 3.162278 3.098387 3.114482 3.114482 2.720294 3.033150 2.966479 3.049590
##   [9] 3.082207 2.966479 3.130495 2.645751 2.966479 2.966479 3.098387 2.949576
##  [17] 2.449490 2.701851 2.774887 2.949576 2.880972 3.098387 3.000000 2.983287
##  [25] 2.408319 3.033150 2.863564 2.915476 2.549510 2.683282 2.983287 2.880972
##  [33] 2.898275 2.880972 2.846050 2.932576 2.932576 2.701851 3.049590 2.607681
##  [41] 2.774887 3.016621 3.098387 2.683282 2.863564 2.664583 2.720294 3.065942
##  [49] 3.098387 2.588436 2.898275 2.121320 2.190890 2.529822 2.846050 2.664583
##  [57] 2.626785 2.949576 2.792848 2.720294 2.810694 2.236068 2.949576 2.664583
##  [65] 2.932576 2.932576 2.863564 2.509980 2.387467 2.529822 2.949576 2.645751
##  [73] 2.915476 2.828427 2.756810 1.949359 2.469818 2.828427 2.167948 2.720294
##  [81] 2.626785 2.144761 2.236068 2.190890 2.626785 2.626785 2.588436 2.408319
##  [89] 2.607681 2.097618 2.469818 2.024846 1.949359 2.323790 2.073644 2.323790
##  [97] 2.073644 2.645751 2.588436 2.073644 2.167948 2.024846 2.549510 2.366432
## [105] 2.387467 2.280351 2.720294 2.190890 2.408319 2.529822 2.024846 2.846050
## [113] 2.049390 2.489980 2.236068 2.345208 2.428992 2.097618 2.024846 1.843909
## [121] 2.024846 2.792848 2.024846 2.258318 1.673320 1.923538 2.024846 2.049390
## [129] 2.121320 2.024846 2.323790 2.190890 2.097618 2.509980 1.870829 2.213594
## [137] 2.049390 2.190890 2.387467 1.923538 2.280351 1.949359 2.190890 1.816590
## [145] 1.788854 2.144761 2.428992 1.843909 1.732051 1.702939 1.788854 1.897367
## [153] 1.702939 1.760682 1.612452 1.897367 1.843909 1.923538 1.581139 2.049390
## [161] 2.428992 1.760682 1.897367 2.000000 1.581139 1.516575 1.897367 1.949359
## [169] 1.581139 1.732051 1.549193 1.673320 1.095445 1.516575 1.549193 1.048809
## [177] 1.303840 1.224745 1.183216
# do the square root of the elements of the columns in 4 and 5 and then return the data type of the list
class(lapply(fragility23[,4:5],sqrt))
## [1] "list"
# do the square root of the elements of the columns in 4 and 5 and then print the data in the vector format
print(sapply(fragility23[,4:5],sqrt))
##            Total S1: Demographic Pressures
##   [1,] 10.578280                  3.162278
##   [2,] 10.435516                  3.098387
##   [3,] 10.416333                  3.114482
##   [4,] 10.353743                  3.114482
##   [5,] 10.348913                  2.720294
##   [6,] 10.324728                  3.033150
##   [7,] 10.305338                  2.966479
##   [8,] 10.281051                  3.049590
##   [9,] 10.227414                  3.082207
##  [10,] 10.143964                  2.966479
##  [11,] 10.019980                  3.130495
##  [12,] 10.009995                  2.645751
##  [13,]  9.974969                  2.966479
##  [14,]  9.924717                  2.966479
##  [15,]  9.899495                  3.098387
##  [16,]  9.843780                  2.949576
##  [17,]  9.803061                  2.449490
##  [18,]  9.792855                  2.701851
##  [19,]  9.721111                  2.774887
##  [20,]  9.705668                  2.949576
##  [21,]  9.695360                  2.880972
##  [22,]  9.695360                  3.098387
##  [23,]  9.695360                  3.000000
##  [24,]  9.664368                  2.983287
##  [25,]  9.581232                  2.408319
##  [26,]  9.565563                  3.033150
##  [27,]  9.560335                  2.863564
##  [28,]  9.523655                  2.915476
##  [29,]  9.513149                  2.549510
##  [30,]  9.502631                  2.683282
##  [31,]  9.481561                  2.983287
##  [32,]  9.481561                  2.880972
##  [33,]  9.428680                  2.898275
##  [34,]  9.375500                  2.880972
##  [35,]  9.370165                  2.846050
##  [36,]  9.332738                  2.932576
##  [37,]  9.327379                  2.932576
##  [38,]  9.327379                  2.701851
##  [39,]  9.322017                  3.049590
##  [40,]  9.241212                  2.607681
##  [41,]  9.230385                  2.774887
##  [42,]  9.186947                  3.016621
##  [43,]  9.121403                  3.098387
##  [44,]  9.071935                  2.683282
##  [45,]  9.066422                  2.863564
##  [46,]  9.066422                  2.664583
##  [47,]  9.060905                  2.720294
##  [48,]  9.044335                  3.065942
##  [49,]  9.038805                  3.098387
##  [50,]  9.033272                  2.588436
##  [51,]  9.022195                  2.898275
##  [52,]  9.011104                  2.121320
##  [53,]  8.983318                  2.190890
##  [54,]  8.961027                  2.529822
##  [55,]  8.955445                  2.846050
##  [56,]  8.921883                  2.664583
##  [57,]  8.921883                  2.626785
##  [58,]  8.893818                  2.949576
##  [59,]  8.837420                  2.792848
##  [60,]  8.837420                  2.720294
##  [61,]  8.820431                  2.810694
##  [62,]  8.814760                  2.236068
##  [63,]  8.803408                  2.949576
##  [64,]  8.792042                  2.664583
##  [65,]  8.752143                  2.932576
##  [66,]  8.734987                  2.932576
##  [67,]  8.723531                  2.863564
##  [68,]  8.700575                  2.509980
##  [69,]  8.694826                  2.387467
##  [70,]  8.642916                  2.529822
##  [71,]  8.631338                  2.949576
##  [72,]  8.613942                  2.645751
##  [73,]  8.608136                  2.915476
##  [74,]  8.561542                  2.828427
##  [75,]  8.549854                  2.756810
##  [76,]  8.526429                  1.949359
##  [77,]  8.502941                  2.469818
##  [78,]  8.485281                  2.828427
##  [79,]  8.479387                  2.167948
##  [80,]  8.455767                  2.720294
##  [81,]  8.408329                  2.626785
##  [82,]  8.378544                  2.144761
##  [83,]  8.366600                  2.236068
##  [84,]  8.360622                  2.190890
##  [85,]  8.354639                  2.626785
##  [86,]  8.348653                  2.626785
##  [87,]  8.330666                  2.588436
##  [88,]  8.324662                  2.408319
##  [89,]  8.324662                  2.607681
##  [90,]  8.258329                  2.097618
##  [91,]  8.246211                  2.469818
##  [92,]  8.240146                  2.024846
##  [93,]  8.215838                  1.949359
##  [94,]  8.209750                  2.323790
##  [95,]  8.173127                  2.073644
##  [96,]  8.148620                  2.323790
##  [97,]  8.148620                  2.073644
##  [98,]  8.099383                  2.645751
##  [99,]  8.093207                  2.588436
## [100,]  8.080842                  2.073644
## [101,]  8.068457                  2.167948
## [102,]  8.068457                  2.024846
## [103,]  8.068457                  2.549510
## [104,]  8.031189                  2.366432
## [105,]  7.981228                  2.387467
## [106,]  7.930952                  2.280351
## [107,]  7.893035                  2.720294
## [108,]  7.867655                  2.190890
## [109,]  7.848567                  2.408319
## [110,]  7.797435                  2.529822
## [111,]  7.784600                  2.024846
## [112,]  7.765307                  2.846050
## [113,]  7.765307                  2.049390
## [114,]  7.752419                  2.489980
## [115,]  7.733046                  2.236068
## [116,]  7.726578                  2.345208
## [117,]  7.713624                  2.428992
## [118,]  7.635444                  2.097618
## [119,]  7.615773                  2.024846
## [120,]  7.549834                  1.843909
## [121,]  7.536577                  2.024846
## [122,]  7.436397                  2.792848
## [123,]  7.422937                  2.024846
## [124,]  7.416198                  2.258318
## [125,]  7.395945                  1.673320
## [126,]  7.334848                  1.923538
## [127,]  7.328028                  2.024846
## [128,]  7.300685                  2.049390
## [129,]  7.280110                  2.121320
## [130,]  7.273239                  2.024846
## [131,]  7.197222                  2.323790
## [132,]  7.162402                  2.190890
## [133,]  7.155418                  2.097618
## [134,]  7.014271                  2.509980
## [135,]  6.985700                  1.870829
## [136,]  6.978539                  2.213594
## [137,]  6.978539                  2.049390
## [138,]  6.978539                  2.190890
## [139,]  6.811755                  2.387467
## [140,]  6.737952                  1.923538
## [141,]  6.730527                  2.280351
## [142,]  6.723095                  1.949359
## [143,]  6.640783                  2.190890
## [144,]  6.595453                  1.816590
## [145,]  6.580274                  1.788854
## [146,]  6.526868                  2.144761
## [147,]  6.496153                  2.428992
## [148,]  6.473021                  1.843909
## [149,]  6.363961                  1.732051
## [150,]  6.356099                  1.702939
## [151,]  6.340347                  1.788854
## [152,]  6.276942                  1.897367
## [153,]  6.212890                  1.702939
## [154,]  6.164414                  1.760682
## [155,]  6.148170                  1.612452
## [156,]  6.082763                  1.897367
## [157,]  5.865151                  1.843909
## [158,]  5.744563                  1.923538
## [159,]  5.612486                  1.581139
## [160,]  5.603570                  2.049390
## [161,]  5.522681                  2.428992
## [162,]  5.366563                  1.760682
## [163,]  5.224940                  1.897367
## [164,]  5.069517                  2.000000
## [165,]  5.049752                  1.581139
## [166,]  4.959839                  1.516575
## [167,]  4.939636                  1.897367
## [168,]  4.690416                  1.949359
## [169,]  4.582576                  1.581139
## [170,]  4.538722                  1.732051
## [171,]  4.415880                  1.549193
## [172,]  4.415880                  1.673320
## [173,]  4.347413                  1.095445
## [174,]  4.230839                  1.516575
## [175,]  4.219005                  1.549193
## [176,]  4.086563                  1.048809
## [177,]  4.000000                  1.303840
## [178,]  3.962323                  1.224745
## [179,]  3.807887                  1.183216
# do the square root of the elements of the columns in 4 and 5 and then return the type of the vector
class(sapply(fragility23[,4:5],sqrt))
## [1] "matrix" "array"

Now our own function:

# create a function called theOnesOK with input parameter DF_country_and variable
theOnesOK = function(DF_country_and_variable) {
  # take the 2nd column of the input list and save it under variable_values
  variable_values <- DF_country_and_variable[,2]
  # calculate the average of the elements that do not have any missing data and save it under avg_value  
  avg_value <- mean(variable_values, na.rm = TRUE)
  # if the value is greater than the average, label it as Above Average. Otherwise label it as Below/At Average. 
  is_above <- ifelse(variable_values > avg_value, "Above Average", "Below/At Average")
  # create a new column called Status and store all the Above Average data
  DF_country_and_variable$Status <- is_above
  # return the orginal list with the new status column 
  return(DF_country_and_variable)
}
# call the function theOnesOK with columns Country and Demographic Pressures 
theOnesOK(fragility23[,c('Country','S1: Demographic Pressures')])
##                       Country S1: Demographic Pressures           Status
## 1                     Somalia                      10.0    Above Average
## 2                       Yemen                       9.6    Above Average
## 3                 South Sudan                       9.7    Above Average
## 4   Congo Democratic Republic                       9.7    Above Average
## 5                       Syria                       7.4    Above Average
## 6                 Afghanistan                       9.2    Above Average
## 7                       Sudan                       8.8    Above Average
## 8    Central African Republic                       9.3    Above Average
## 9                        Chad                       9.5    Above Average
## 10                      Haiti                       8.8    Above Average
## 11                   Ethiopia                       9.8    Above Average
## 12                    Myanmar                       7.0    Above Average
## 13                       Mali                       8.8    Above Average
## 14                     Guinea                       8.8    Above Average
## 15                    Nigeria                       9.6    Above Average
## 16                   Zimbabwe                       8.7    Above Average
## 17                      Libya                       6.0    Above Average
## 18                    Ukraine                       7.3    Above Average
## 19                    Eritrea                       7.7    Above Average
## 20                    Burundi                       8.7    Above Average
## 21               Burkina Faso                       8.3    Above Average
## 22                 Mozambique                       9.6    Above Average
## 23                   Cameroon                       9.0    Above Average
## 24                      Niger                       8.9    Above Average
## 25                    Lebanon                       5.8 Below/At Average
## 26                     Uganda                       9.2    Above Average
## 27                       Iraq                       8.2    Above Average
## 28             Congo Republic                       8.5    Above Average
## 29                  Venezuela                       6.5    Above Average
## 30                  Sri Lanka                       7.2    Above Average
## 31              Guinea Bissau                       8.9    Above Average
## 32                   Pakistan                       8.3    Above Average
## 33                    Liberia                       8.4    Above Average
## 34                  Palestine                       8.3    Above Average
## 35                      Kenya                       8.1    Above Average
## 36              Cote d'Ivoire                       8.6    Above Average
## 37                 Mauritania                       8.6    Above Average
## 38                North Korea                       7.3    Above Average
## 39                     Angola                       9.3    Above Average
## 40                       Iran                       6.8    Above Average
## 41                 Bangladesh                       7.7    Above Average
## 42          Equatorial Guinea                       9.1    Above Average
## 43                     Malawi                       9.6    Above Average
## 44                     Rwanda                       7.2    Above Average
## 45                    Comoros                       8.2    Above Average
## 46                   Djibouti                       7.1    Above Average
## 47                       Togo                       7.4    Above Average
## 48                     Zambia                       9.4    Above Average
## 49                 Madagascar                       9.6    Above Average
## 50                      Egypt                       6.7    Above Average
## 51               Sierra Leone                       8.4    Above Average
## 52                     Turkey                       4.5 Below/At Average
## 53                     Russia                       4.8 Below/At Average
## 54                   Cambodia                       6.4    Above Average
## 55                      Nepal                       8.1    Above Average
## 56            Solomon Islands                       7.1    Above Average
## 57                   Honduras                       6.9    Above Average
## 58                  Swaziland                       8.7    Above Average
## 59                   Colombia                       7.8    Above Average
## 60           Papua New Guinea                       7.4    Above Average
## 61                Philippines                       7.9    Above Average
## 62                  Nicaragua                       5.0 Below/At Average
## 63                Timor-Leste                       8.7    Above Average
## 64                  Guatemala                       7.1    Above Average
## 65                   Tanzania                       8.6    Above Average
## 66                    Lesotho                       8.6    Above Average
## 67                     Gambia                       8.2    Above Average
## 68                     Jordan                       6.3    Above Average
## 69            Kyrgyz Republic                       5.7 Below/At Average
## 70                       Laos                       6.4    Above Average
## 71                     Brazil                       8.7    Above Average
## 72                 Tajikistan                       7.0    Above Average
## 73                      India                       8.5    Above Average
## 74                      Benin                       8.0    Above Average
## 75                       Peru                       7.6    Above Average
## 76                 Azerbaijan                       3.8 Below/At Average
## 77     Bosnia and Herzegovina                       6.1    Above Average
## 78               South Africa                       8.0    Above Average
## 79                    Georgia                       4.7 Below/At Average
## 80                    Senegal                       7.4    Above Average
## 81                    Bolivia                       6.9    Above Average
## 82                       Fiji                       4.6 Below/At Average
## 83                    Algeria                       5.0 Below/At Average
## 84                    Belarus                       4.8 Below/At Average
## 85                     Mexico                       6.9    Above Average
## 86      Sao Tome and Principe                       6.9    Above Average
## 87                    Ecuador                       6.7    Above Average
## 88                 Micronesia                       5.8 Below/At Average
## 89                El Salvador                       6.8    Above Average
## 90                    Morocco                       4.4 Below/At Average
## 91                   Thailand                       6.1    Above Average
## 92                     Serbia                       4.1 Below/At Average
## 93                    Armenia                       3.8 Below/At Average
## 94                    Moldova                       5.4 Below/At Average
## 95                 Uzbekistan                       4.3 Below/At Average
## 96                     Bhutan                       5.4 Below/At Average
## 97                    Tunisia                       4.3 Below/At Average
## 98                  Indonesia                       7.0    Above Average
## 99                      Gabon                       6.7    Above Average
## 100              Saudi Arabia                       4.3 Below/At Average
## 101                     Samoa                       4.7 Below/At Average
## 102                   Bahrain                       4.1 Below/At Average
## 103                     China                       6.5    Above Average
## 104              Turkmenistan                       5.6 Below/At Average
## 105                  Paraguay                       5.7 Below/At Average
## 106                  Maldives                       5.2 Below/At Average
## 107                     Ghana                       7.4    Above Average
## 108                   Jamaica                       4.8 Below/At Average
## 109                    Guyana                       5.8 Below/At Average
## 110        Dominican Republic                       6.4    Above Average
## 111                Kazakhstan                       4.1 Below/At Average
## 112                   Namibia                       8.1    Above Average
## 113                 Macedonia                       4.2 Below/At Average
## 114                Cape Verde                       6.2    Above Average
## 115                    Belize                       5.0 Below/At Average
## 116                  Suriname                       5.5 Below/At Average
## 117                      Cuba                       5.9 Below/At Average
## 118                   Vietnam                       4.4 Below/At Average
## 119                Montenegro                       4.1 Below/At Average
## 120                    Cyprus                       3.4 Below/At Average
## 121                   Albania                       4.1 Below/At Average
## 122                  Botswana                       7.8    Above Average
## 123                    Greece                       4.1 Below/At Average
## 124                  Malaysia                       5.1 Below/At Average
## 125         Brunei Darussalam                       2.8 Below/At Average
## 126       Antigua and Barbuda                       3.7 Below/At Average
## 127                   Grenada                       4.1 Below/At Average
## 128                Seychelles                       4.2 Below/At Average
## 129                   Romania                       4.5 Below/At Average
## 130       Trinidad and Tobago                       4.1 Below/At Average
## 131                  Bulgaria                       5.4 Below/At Average
## 132                  Mongolia                       4.8 Below/At Average
## 133                    Kuwait                       4.4 Below/At Average
## 134                   Bahamas                       6.3    Above Average
## 135                   Hungary                       3.5 Below/At Average
## 136                    Panama                       4.9 Below/At Average
## 137                      Oman                       4.2 Below/At Average
## 138                   Croatia                       4.8 Below/At Average
## 139                 Argentina                       5.7 Below/At Average
## 140                  Barbados                       3.7 Below/At Average
## 141             United States                       5.2 Below/At Average
## 142                    Poland                       3.8 Below/At Average
## 143                    Israel                       4.8 Below/At Average
## 144                     Spain                       3.3 Below/At Average
## 145                    Latvia                       3.2 Below/At Average
## 146                     Italy                       4.6 Below/At Average
## 147                     Chile                       5.9 Below/At Average
## 148            United Kingdom                       3.4 Below/At Average
## 149                     Qatar                       3.0 Below/At Average
## 150                Costa Rica                       2.9 Below/At Average
## 151            Czech Republic                       3.2 Below/At Average
## 152                 Lithuania                       3.6 Below/At Average
## 153                   Estonia                       2.9 Below/At Average
## 154                 Mauritius                       3.1 Below/At Average
## 155           Slovak Republic                       2.6 Below/At Average
## 156      United Arab Emirates                       3.6 Below/At Average
## 157                   Uruguay                       3.4 Below/At Average
## 158                     Malta                       3.7 Below/At Average
## 159               South Korea                       2.5 Below/At Average
## 160                   Belgium                       4.2 Below/At Average
## 161                     Japan                       5.9 Below/At Average
## 162                    France                       3.1 Below/At Average
## 163                  Slovenia                       3.6 Below/At Average
## 164                  Portugal                       4.0 Below/At Average
## 165                 Singapore                       2.5 Below/At Average
## 166                   Germany                       2.3 Below/At Average
## 167                   Austria                       3.6 Below/At Average
## 168                 Australia                       3.8 Below/At Average
## 169               Netherlands                       2.5 Below/At Average
## 170                    Sweden                       3.0 Below/At Average
## 171                Luxembourg                       2.4 Below/At Average
## 172                   Ireland                       2.8 Below/At Average
## 173                    Canada                       1.2 Below/At Average
## 174                   Denmark                       2.3 Below/At Average
## 175               Switzerland                       2.4 Below/At Average
## 176               New Zealand                       1.1 Below/At Average
## 177                   Finland                       1.7 Below/At Average
## 178                   Iceland                       1.5 Below/At Average
## 179                    Norway                       1.4 Below/At Average

the function to compute the row wise average across the given columns and return the new data frame with country column and the average

# create a function with the name mystery with input parameters data frame, vector of columns and the country column which has as a default value of country
mystery=function(DF,positionsToUse,CountryColumn='Country'){
  # extract the country column from DF and ignore it if its not a vector
  newDF=DF[,c(CountryColumn),drop = FALSE]
  # create a new column named average 
  average='average'
  # calculate the average for each row and save it under the new column average
  newDF[,average]=apply(DF[,positionsToUse],1,mean,na.rm = TRUE)
  # return the country identifier as well as row wise average
  return(newDF[,c(CountryColumn,average)])
}
# extract the specific columns total and S1
fragility23[,c('Total', 'S1: Demographic Pressures')]
##     Total S1: Demographic Pressures
## 1   111.9                      10.0
## 2   108.9                       9.6
## 3   108.5                       9.7
## 4   107.2                       9.7
## 5   107.1                       7.4
## 6   106.6                       9.2
## 7   106.2                       8.8
## 8   105.7                       9.3
## 9   104.6                       9.5
## 10  102.9                       8.8
## 11  100.4                       9.8
## 12  100.2                       7.0
## 13   99.5                       8.8
## 14   98.5                       8.8
## 15   98.0                       9.6
## 16   96.9                       8.7
## 17   96.1                       6.0
## 18   95.9                       7.3
## 19   94.5                       7.7
## 20   94.2                       8.7
## 21   94.0                       8.3
## 22   94.0                       9.6
## 23   94.0                       9.0
## 24   93.4                       8.9
## 25   91.8                       5.8
## 26   91.5                       9.2
## 27   91.4                       8.2
## 28   90.7                       8.5
## 29   90.5                       6.5
## 30   90.3                       7.2
## 31   89.9                       8.9
## 32   89.9                       8.3
## 33   88.9                       8.4
## 34   87.9                       8.3
## 35   87.8                       8.1
## 36   87.1                       8.6
## 37   87.0                       8.6
## 38   87.0                       7.3
## 39   86.9                       9.3
## 40   85.4                       6.8
## 41   85.2                       7.7
## 42   84.4                       9.1
## 43   83.2                       9.6
## 44   82.3                       7.2
## 45   82.2                       8.2
## 46   82.2                       7.1
## 47   82.1                       7.4
## 48   81.8                       9.4
## 49   81.7                       9.6
## 50   81.6                       6.7
## 51   81.4                       8.4
## 52   81.2                       4.5
## 53   80.7                       4.8
## 54   80.3                       6.4
## 55   80.2                       8.1
## 56   79.6                       7.1
## 57   79.6                       6.9
## 58   79.1                       8.7
## 59   78.1                       7.8
## 60   78.1                       7.4
## 61   77.8                       7.9
## 62   77.7                       5.0
## 63   77.5                       8.7
## 64   77.3                       7.1
## 65   76.6                       8.6
## 66   76.3                       8.6
## 67   76.1                       8.2
## 68   75.7                       6.3
## 69   75.6                       5.7
## 70   74.7                       6.4
## 71   74.5                       8.7
## 72   74.2                       7.0
## 73   74.1                       8.5
## 74   73.3                       8.0
## 75   73.1                       7.6
## 76   72.7                       3.8
## 77   72.3                       6.1
## 78   72.0                       8.0
## 79   71.9                       4.7
## 80   71.5                       7.4
## 81   70.7                       6.9
## 82   70.2                       4.6
## 83   70.0                       5.0
## 84   69.9                       4.8
## 85   69.8                       6.9
## 86   69.7                       6.9
## 87   69.4                       6.7
## 88   69.3                       5.8
## 89   69.3                       6.8
## 90   68.2                       4.4
## 91   68.0                       6.1
## 92   67.9                       4.1
## 93   67.5                       3.8
## 94   67.4                       5.4
## 95   66.8                       4.3
## 96   66.4                       5.4
## 97   66.4                       4.3
## 98   65.6                       7.0
## 99   65.5                       6.7
## 100  65.3                       4.3
## 101  65.1                       4.7
## 102  65.1                       4.1
## 103  65.1                       6.5
## 104  64.5                       5.6
## 105  63.7                       5.7
## 106  62.9                       5.2
## 107  62.3                       7.4
## 108  61.9                       4.8
## 109  61.6                       5.8
## 110  60.8                       6.4
## 111  60.6                       4.1
## 112  60.3                       8.1
## 113  60.3                       4.2
## 114  60.1                       6.2
## 115  59.8                       5.0
## 116  59.7                       5.5
## 117  59.5                       5.9
## 118  58.3                       4.4
## 119  58.0                       4.1
## 120  57.0                       3.4
## 121  56.8                       4.1
## 122  55.3                       7.8
## 123  55.1                       4.1
## 124  55.0                       5.1
## 125  54.7                       2.8
## 126  53.8                       3.7
## 127  53.7                       4.1
## 128  53.3                       4.2
## 129  53.0                       4.5
## 130  52.9                       4.1
## 131  51.8                       5.4
## 132  51.3                       4.8
## 133  51.2                       4.4
## 134  49.2                       6.3
## 135  48.8                       3.5
## 136  48.7                       4.9
## 137  48.7                       4.2
## 138  48.7                       4.8
## 139  46.4                       5.7
## 140  45.4                       3.7
## 141  45.3                       5.2
## 142  45.2                       3.8
## 143  44.1                       4.8
## 144  43.5                       3.3
## 145  43.3                       3.2
## 146  42.6                       4.6
## 147  42.2                       5.9
## 148  41.9                       3.4
## 149  40.5                       3.0
## 150  40.4                       2.9
## 151  40.2                       3.2
## 152  39.4                       3.6
## 153  38.6                       2.9
## 154  38.0                       3.1
## 155  37.8                       2.6
## 156  37.0                       3.6
## 157  34.4                       3.4
## 158  33.0                       3.7
## 159  31.5                       2.5
## 160  31.4                       4.2
## 161  30.5                       5.9
## 162  28.8                       3.1
## 163  27.3                       3.6
## 164  25.7                       4.0
## 165  25.5                       2.5
## 166  24.6                       2.3
## 167  24.4                       3.6
## 168  22.0                       3.8
## 169  21.0                       2.5
## 170  20.6                       3.0
## 171  19.5                       2.4
## 172  19.5                       2.8
## 173  18.9                       1.2
## 174  17.9                       2.3
## 175  17.8                       2.4
## 176  16.7                       1.1
## 177  16.0                       1.7
## 178  15.7                       1.5
## 179  14.5                       1.4

New Function theOnesOK2

function will label each row as above or below the average based on the chosen variable

in the original theOnesOK function, the row was being labeled as above or below based on column 2 but not on the chosen variable

# create a function by the name theOnesOk2 
theOnesOK2 = function(DF, DFvariable, CountryColumn='Country') {
  # extract the variable and save it in variable_values
  variable_values <- DF[,DFvariable]
  # calculate the average of the elements listed in variable values
  avg_value <- mean(variable_values, na.rm = TRUE)
  # if the value is greater than the average, label it as Above Average. Otherwise label it as Below/At Average. 
  is_above <- ifelse(variable_values > avg_value, "Above Average", "Below/At Average")
  # create a new column name as Status_on <column name>
  newname = paste('Status_on', DFvariable)
  # add the new status into the data frame 
  DF[,newname] <- is_above 
  # return the country identifier along with the status column 
  return(DF[,c(CountryColumn, newname)])
}