Filter data

A recurring task in data analytics is to filter data, this means to select specific subsets/chucks of your data. Filters can be used to create new variables, to apply new functions, to see specific data, etc. There are numerous ways to filter data in R, but here we wall use the function filter in the package “dplyr”.

# install.packages("dplyr")     #First, I install the package dplyr and tidyverse, since I have not installed it previously
# install.packages("tidyverse") 

library(tidyverse) #next I load the packages into the current section of R.
library(dplyr)            

Now, lets check the first few rows in the GDP database we loaded earlier,

tail(GDPData)             #lets look at the column head of the GDP data we loaded earlier.
##       country continent year gdpPercap
## 1699 Zimbabwe    Africa 1982  788.8550
## 1700 Zimbabwe    Africa 1987  706.1573
## 1701 Zimbabwe    Africa 1992  693.4208
## 1702 Zimbabwe    Africa 1997  792.4500
## 1703 Zimbabwe    Africa 2002  672.0386
## 1704 Zimbabwe    Africa 2007  469.7093

From the subset of the data above, you can see that the GDPData database has four columns, and from the “year” column, you can tell that the data of GDP (gdpPercap) is repeated for each year. Hmm, but what if I just want to see the data for countries in Asia?. For that we use the function filter.

The syntax of the filter function reads like this:

Filter function

Figure 3.7: Filter function

Pipes, %>%, are a powerful tool for doing sequential operations. Commonly it requires tthe tidyverse package. In the next section you will better see the use of pipes.

Now that you know the syntax of the filter function from the dplr package, lets test it.

GDPData %>% filter(continent=="Asia")       
##                country continent year   gdpPercap
## 1          Afghanistan      Asia 1952    779.4453
## 2          Afghanistan      Asia 1957    820.8530
## 3          Afghanistan      Asia 1962    853.1007
## 4          Afghanistan      Asia 1967    836.1971
## 5          Afghanistan      Asia 1972    739.9811
## 6          Afghanistan      Asia 1977    786.1134
## 7          Afghanistan      Asia 1982    978.0114
## 8          Afghanistan      Asia 1987    852.3959
## 9          Afghanistan      Asia 1992    649.3414
## 10         Afghanistan      Asia 1997    635.3414
## 11         Afghanistan      Asia 2002    726.7341
## 12         Afghanistan      Asia 2007    974.5803
## 13             Bahrain      Asia 1952   9867.0848
## 14             Bahrain      Asia 1957  11635.7995
## 15             Bahrain      Asia 1962  12753.2751
## 16             Bahrain      Asia 1967  14804.6727
## 17             Bahrain      Asia 1972  18268.6584
## 18             Bahrain      Asia 1977  19340.1020
## 19             Bahrain      Asia 1982  19211.1473
## 20             Bahrain      Asia 1987  18524.0241
## 21             Bahrain      Asia 1992  19035.5792
## 22             Bahrain      Asia 1997  20292.0168
## 23             Bahrain      Asia 2002  23403.5593
## 24             Bahrain      Asia 2007  29796.0483
## 25          Bangladesh      Asia 1952    684.2442
## 26          Bangladesh      Asia 1957    661.6375
## 27          Bangladesh      Asia 1962    686.3416
## 28          Bangladesh      Asia 1967    721.1861
## 29          Bangladesh      Asia 1972    630.2336
## 30          Bangladesh      Asia 1977    659.8772
## 31          Bangladesh      Asia 1982    676.9819
## 32          Bangladesh      Asia 1987    751.9794
## 33          Bangladesh      Asia 1992    837.8102
## 34          Bangladesh      Asia 1997    972.7700
## 35          Bangladesh      Asia 2002   1136.3904
## 36          Bangladesh      Asia 2007   1391.2538
## 37            Cambodia      Asia 1952    368.4693
## 38            Cambodia      Asia 1957    434.0383
## 39            Cambodia      Asia 1962    496.9136
## 40            Cambodia      Asia 1967    523.4323
## 41            Cambodia      Asia 1972    421.6240
## 42            Cambodia      Asia 1977    524.9722
## 43            Cambodia      Asia 1982    624.4755
## 44            Cambodia      Asia 1987    683.8956
## 45            Cambodia      Asia 1992    682.3032
## 46            Cambodia      Asia 1997    734.2852
## 47            Cambodia      Asia 2002    896.2260
## 48            Cambodia      Asia 2007   1713.7787
## 49               China      Asia 1952    400.4486
## 50               China      Asia 1957    575.9870
## 51               China      Asia 1962    487.6740
## 52               China      Asia 1967    612.7057
## 53               China      Asia 1972    676.9001
## 54               China      Asia 1977    741.2375
## 55               China      Asia 1982    962.4214
## 56               China      Asia 1987   1378.9040
## 57               China      Asia 1992   1655.7842
## 58               China      Asia 1997   2289.2341
## 59               China      Asia 2002   3119.2809
## 60               China      Asia 2007   4959.1149
## 61    Hong Kong, China      Asia 1952   3054.4212
## 62    Hong Kong, China      Asia 1957   3629.0765
## 63    Hong Kong, China      Asia 1962   4692.6483
## 64    Hong Kong, China      Asia 1967   6197.9628
## 65    Hong Kong, China      Asia 1972   8315.9281
## 66    Hong Kong, China      Asia 1977  11186.1413
## 67    Hong Kong, China      Asia 1982  14560.5305
## 68    Hong Kong, China      Asia 1987  20038.4727
## 69    Hong Kong, China      Asia 1992  24757.6030
## 70    Hong Kong, China      Asia 1997  28377.6322
## 71    Hong Kong, China      Asia 2002  30209.0152
## 72    Hong Kong, China      Asia 2007  39724.9787
## 73               India      Asia 1952    546.5657
## 74               India      Asia 1957    590.0620
## 75               India      Asia 1962    658.3472
## 76               India      Asia 1967    700.7706
## 77               India      Asia 1972    724.0325
## 78               India      Asia 1977    813.3373
## 79               India      Asia 1982    855.7235
## 80               India      Asia 1987    976.5127
## 81               India      Asia 1992   1164.4068
## 82               India      Asia 1997   1458.8174
## 83               India      Asia 2002   1746.7695
## 84               India      Asia 2007   2452.2104
## 85           Indonesia      Asia 1952    749.6817
## 86           Indonesia      Asia 1957    858.9003
## 87           Indonesia      Asia 1962    849.2898
## 88           Indonesia      Asia 1967    762.4318
## 89           Indonesia      Asia 1972   1111.1079
## 90           Indonesia      Asia 1977   1382.7021
## 91           Indonesia      Asia 1982   1516.8730
## 92           Indonesia      Asia 1987   1748.3570
## 93           Indonesia      Asia 1992   2383.1409
## 94           Indonesia      Asia 1997   3119.3356
## 95           Indonesia      Asia 2002   2873.9129
## 96           Indonesia      Asia 2007   3540.6516
## 97                Iran      Asia 1952   3035.3260
## 98                Iran      Asia 1957   3290.2576
## 99                Iran      Asia 1962   4187.3298
## 100               Iran      Asia 1967   5906.7318
## 101               Iran      Asia 1972   9613.8186
## 102               Iran      Asia 1977  11888.5951
## 103               Iran      Asia 1982   7608.3346
## 104               Iran      Asia 1987   6642.8814
## 105               Iran      Asia 1992   7235.6532
## 106               Iran      Asia 1997   8263.5903
## 107               Iran      Asia 2002   9240.7620
## 108               Iran      Asia 2007  11605.7145
## 109               Iraq      Asia 1952   4129.7661
## 110               Iraq      Asia 1957   6229.3336
## 111               Iraq      Asia 1962   8341.7378
## 112               Iraq      Asia 1967   8931.4598
## 113               Iraq      Asia 1972   9576.0376
## 114               Iraq      Asia 1977  14688.2351
## 115               Iraq      Asia 1982  14517.9071
## 116               Iraq      Asia 1987  11643.5727
## 117               Iraq      Asia 1992   3745.6407
## 118               Iraq      Asia 1997   3076.2398
## 119               Iraq      Asia 2002   4390.7173
## 120               Iraq      Asia 2007   4471.0619
## 121             Israel      Asia 1952   4086.5221
## 122             Israel      Asia 1957   5385.2785
## 123             Israel      Asia 1962   7105.6307
## 124             Israel      Asia 1967   8393.7414
## 125             Israel      Asia 1972  12786.9322
## 126             Israel      Asia 1977  13306.6192
## 127             Israel      Asia 1982  15367.0292
## 128             Israel      Asia 1987  17122.4799
## 129             Israel      Asia 1992  18051.5225
## 130             Israel      Asia 1997  20896.6092
## 131             Israel      Asia 2002  21905.5951
## 132             Israel      Asia 2007  25523.2771
## 133              Japan      Asia 1952   3216.9563
## 134              Japan      Asia 1957   4317.6944
## 135              Japan      Asia 1962   6576.6495
## 136              Japan      Asia 1967   9847.7886
## 137              Japan      Asia 1972  14778.7864
## 138              Japan      Asia 1977  16610.3770
## 139              Japan      Asia 1982  19384.1057
## 140              Japan      Asia 1987  22375.9419
## 141              Japan      Asia 1992  26824.8951
## 142              Japan      Asia 1997  28816.5850
## 143              Japan      Asia 2002  28604.5919
## 144              Japan      Asia 2007  31656.0681
## 145             Jordan      Asia 1952   1546.9078
## 146             Jordan      Asia 1957   1886.0806
## 147             Jordan      Asia 1962   2348.0092
## 148             Jordan      Asia 1967   2741.7963
## 149             Jordan      Asia 1972   2110.8563
## 150             Jordan      Asia 1977   2852.3516
## 151             Jordan      Asia 1982   4161.4160
## 152             Jordan      Asia 1987   4448.6799
## 153             Jordan      Asia 1992   3431.5936
## 154             Jordan      Asia 1997   3645.3796
## 155             Jordan      Asia 2002   3844.9172
## 156             Jordan      Asia 2007   4519.4612
## 157   Korea, Dem. Rep.      Asia 1952   1088.2778
## 158   Korea, Dem. Rep.      Asia 1957   1571.1347
## 159   Korea, Dem. Rep.      Asia 1962   1621.6936
## 160   Korea, Dem. Rep.      Asia 1967   2143.5406
## 161   Korea, Dem. Rep.      Asia 1972   3701.6215
## 162   Korea, Dem. Rep.      Asia 1977   4106.3012
## 163   Korea, Dem. Rep.      Asia 1982   4106.5253
## 164   Korea, Dem. Rep.      Asia 1987   4106.4923
## 165   Korea, Dem. Rep.      Asia 1992   3726.0635
## 166   Korea, Dem. Rep.      Asia 1997   1690.7568
## 167   Korea, Dem. Rep.      Asia 2002   1646.7582
## 168   Korea, Dem. Rep.      Asia 2007   1593.0655
## 169        Korea, Rep.      Asia 1952   1030.5922
## 170        Korea, Rep.      Asia 1957   1487.5935
## 171        Korea, Rep.      Asia 1962   1536.3444
## 172        Korea, Rep.      Asia 1967   2029.2281
## 173        Korea, Rep.      Asia 1972   3030.8767
## 174        Korea, Rep.      Asia 1977   4657.2210
## 175        Korea, Rep.      Asia 1982   5622.9425
## 176        Korea, Rep.      Asia 1987   8533.0888
## 177        Korea, Rep.      Asia 1992  12104.2787
## 178        Korea, Rep.      Asia 1997  15993.5280
## 179        Korea, Rep.      Asia 2002  19233.9882
## 180        Korea, Rep.      Asia 2007  23348.1397
## 181             Kuwait      Asia 1952 108382.3529
## 182             Kuwait      Asia 1957 113523.1329
## 183             Kuwait      Asia 1962  95458.1118
## 184             Kuwait      Asia 1967  80894.8833
## 185             Kuwait      Asia 1972 109347.8670
## 186             Kuwait      Asia 1977  59265.4771
## 187             Kuwait      Asia 1982  31354.0357
## 188             Kuwait      Asia 1987  28118.4300
## 189             Kuwait      Asia 1992  34932.9196
## 190             Kuwait      Asia 1997  40300.6200
## 191             Kuwait      Asia 2002  35110.1057
## 192             Kuwait      Asia 2007  47306.9898
## 193            Lebanon      Asia 1952   4834.8041
## 194            Lebanon      Asia 1957   6089.7869
## 195            Lebanon      Asia 1962   5714.5606
## 196            Lebanon      Asia 1967   6006.9830
## 197            Lebanon      Asia 1972   7486.3843
## 198            Lebanon      Asia 1977   8659.6968
## 199            Lebanon      Asia 1982   7640.5195
## 200            Lebanon      Asia 1987   5377.0913
## 201            Lebanon      Asia 1992   6890.8069
## 202            Lebanon      Asia 1997   8754.9639
## 203            Lebanon      Asia 2002   9313.9388
## 204            Lebanon      Asia 2007  10461.0587
## 205           Malaysia      Asia 1952   1831.1329
## 206           Malaysia      Asia 1957   1810.0670
## 207           Malaysia      Asia 1962   2036.8849
## 208           Malaysia      Asia 1967   2277.7424
## 209           Malaysia      Asia 1972   2849.0948
## 210           Malaysia      Asia 1977   3827.9216
## 211           Malaysia      Asia 1982   4920.3560
## 212           Malaysia      Asia 1987   5249.8027
## 213           Malaysia      Asia 1992   7277.9128
## 214           Malaysia      Asia 1997  10132.9096
## 215           Malaysia      Asia 2002  10206.9779
## 216           Malaysia      Asia 2007  12451.6558
## 217           Mongolia      Asia 1952    786.5669
## 218           Mongolia      Asia 1957    912.6626
## 219           Mongolia      Asia 1962   1056.3540
## 220           Mongolia      Asia 1967   1226.0411
## 221           Mongolia      Asia 1972   1421.7420
## 222           Mongolia      Asia 1977   1647.5117
## 223           Mongolia      Asia 1982   2000.6031
## 224           Mongolia      Asia 1987   2338.0083
## 225           Mongolia      Asia 1992   1785.4020
## 226           Mongolia      Asia 1997   1902.2521
## 227           Mongolia      Asia 2002   2140.7393
## 228           Mongolia      Asia 2007   3095.7723
## 229            Myanmar      Asia 1952    331.0000
## 230            Myanmar      Asia 1957    350.0000
## 231            Myanmar      Asia 1962    388.0000
## 232            Myanmar      Asia 1967    349.0000
## 233            Myanmar      Asia 1972    357.0000
## 234            Myanmar      Asia 1977    371.0000
## 235            Myanmar      Asia 1982    424.0000
## 236            Myanmar      Asia 1987    385.0000
## 237            Myanmar      Asia 1992    347.0000
## 238            Myanmar      Asia 1997    415.0000
## 239            Myanmar      Asia 2002    611.0000
## 240            Myanmar      Asia 2007    944.0000
## 241              Nepal      Asia 1952    545.8657
## 242              Nepal      Asia 1957    597.9364
## 243              Nepal      Asia 1962    652.3969
## 244              Nepal      Asia 1967    676.4422
## 245              Nepal      Asia 1972    674.7881
## 246              Nepal      Asia 1977    694.1124
## 247              Nepal      Asia 1982    718.3731
## 248              Nepal      Asia 1987    775.6325
## 249              Nepal      Asia 1992    897.7404
## 250              Nepal      Asia 1997   1010.8921
## 251              Nepal      Asia 2002   1057.2063
## 252              Nepal      Asia 2007   1091.3598
## 253               Oman      Asia 1952   1828.2303
## 254               Oman      Asia 1957   2242.7466
## 255               Oman      Asia 1962   2924.6381
## 256               Oman      Asia 1967   4720.9427
## 257               Oman      Asia 1972  10618.0385
## 258               Oman      Asia 1977  11848.3439
## 259               Oman      Asia 1982  12954.7910
## 260               Oman      Asia 1987  18115.2231
## 261               Oman      Asia 1992  18616.7069
## 262               Oman      Asia 1997  19702.0558
## 263               Oman      Asia 2002  19774.8369
## 264               Oman      Asia 2007  22316.1929
## 265           Pakistan      Asia 1952    684.5971
## 266           Pakistan      Asia 1957    747.0835
## 267           Pakistan      Asia 1962    803.3427
## 268           Pakistan      Asia 1967    942.4083
## 269           Pakistan      Asia 1972   1049.9390
## 270           Pakistan      Asia 1977   1175.9212
## 271           Pakistan      Asia 1982   1443.4298
## 272           Pakistan      Asia 1987   1704.6866
## 273           Pakistan      Asia 1992   1971.8295
## 274           Pakistan      Asia 1997   2049.3505
## 275           Pakistan      Asia 2002   2092.7124
## 276           Pakistan      Asia 2007   2605.9476
## 277        Philippines      Asia 1952   1272.8810
## 278        Philippines      Asia 1957   1547.9448
## 279        Philippines      Asia 1962   1649.5522
## 280        Philippines      Asia 1967   1814.1274
## 281        Philippines      Asia 1972   1989.3741
## 282        Philippines      Asia 1977   2373.2043
## 283        Philippines      Asia 1982   2603.2738
## 284        Philippines      Asia 1987   2189.6350
## 285        Philippines      Asia 1992   2279.3240
## 286        Philippines      Asia 1997   2536.5349
## 287        Philippines      Asia 2002   2650.9211
## 288        Philippines      Asia 2007   3190.4810
## 289       Saudi Arabia      Asia 1952   6459.5548
## 290       Saudi Arabia      Asia 1957   8157.5912
## 291       Saudi Arabia      Asia 1962  11626.4197
## 292       Saudi Arabia      Asia 1967  16903.0489
## 293       Saudi Arabia      Asia 1972  24837.4287
## 294       Saudi Arabia      Asia 1977  34167.7626
## 295       Saudi Arabia      Asia 1982  33693.1753
## 296       Saudi Arabia      Asia 1987  21198.2614
## 297       Saudi Arabia      Asia 1992  24841.6178
## 298       Saudi Arabia      Asia 1997  20586.6902
## 299       Saudi Arabia      Asia 2002  19014.5412
## 300       Saudi Arabia      Asia 2007  21654.8319
## 301          Singapore      Asia 1952   2315.1382
## 302          Singapore      Asia 1957   2843.1044
## 303          Singapore      Asia 1962   3674.7356
## 304          Singapore      Asia 1967   4977.4185
## 305          Singapore      Asia 1972   8597.7562
## 306          Singapore      Asia 1977  11210.0895
## 307          Singapore      Asia 1982  15169.1611
## 308          Singapore      Asia 1987  18861.5308
## 309          Singapore      Asia 1992  24769.8912
## 310          Singapore      Asia 1997  33519.4766
## 311          Singapore      Asia 2002  36023.1054
## 312          Singapore      Asia 2007  47143.1796
## 313          Sri Lanka      Asia 1952   1083.5320
## 314          Sri Lanka      Asia 1957   1072.5466
## 315          Sri Lanka      Asia 1962   1074.4720
## 316          Sri Lanka      Asia 1967   1135.5143
## 317          Sri Lanka      Asia 1972   1213.3955
## 318          Sri Lanka      Asia 1977   1348.7757
## 319          Sri Lanka      Asia 1982   1648.0798
## 320          Sri Lanka      Asia 1987   1876.7668
## 321          Sri Lanka      Asia 1992   2153.7392
## 322          Sri Lanka      Asia 1997   2664.4773
## 323          Sri Lanka      Asia 2002   3015.3788
## 324          Sri Lanka      Asia 2007   3970.0954
## 325              Syria      Asia 1952   1643.4854
## 326              Syria      Asia 1957   2117.2349
## 327              Syria      Asia 1962   2193.0371
## 328              Syria      Asia 1967   1881.9236
## 329              Syria      Asia 1972   2571.4230
## 330              Syria      Asia 1977   3195.4846
## 331              Syria      Asia 1982   3761.8377
## 332              Syria      Asia 1987   3116.7743
## 333              Syria      Asia 1992   3340.5428
## 334              Syria      Asia 1997   4014.2390
## 335              Syria      Asia 2002   4090.9253
## 336              Syria      Asia 2007   4184.5481
## 337             Taiwan      Asia 1952   1206.9479
## 338             Taiwan      Asia 1957   1507.8613
## 339             Taiwan      Asia 1962   1822.8790
## 340             Taiwan      Asia 1967   2643.8587
## 341             Taiwan      Asia 1972   4062.5239
## 342             Taiwan      Asia 1977   5596.5198
## 343             Taiwan      Asia 1982   7426.3548
## 344             Taiwan      Asia 1987  11054.5618
## 345             Taiwan      Asia 1992  15215.6579
## 346             Taiwan      Asia 1997  20206.8210
## 347             Taiwan      Asia 2002  23235.4233
## 348             Taiwan      Asia 2007  28718.2768
## 349           Thailand      Asia 1952    757.7974
## 350           Thailand      Asia 1957    793.5774
## 351           Thailand      Asia 1962   1002.1992
## 352           Thailand      Asia 1967   1295.4607
## 353           Thailand      Asia 1972   1524.3589
## 354           Thailand      Asia 1977   1961.2246
## 355           Thailand      Asia 1982   2393.2198
## 356           Thailand      Asia 1987   2982.6538
## 357           Thailand      Asia 1992   4616.8965
## 358           Thailand      Asia 1997   5852.6255
## 359           Thailand      Asia 2002   5913.1875
## 360           Thailand      Asia 2007   7458.3963
## 361            Vietnam      Asia 1952    605.0665
## 362            Vietnam      Asia 1957    676.2854
## 363            Vietnam      Asia 1962    772.0492
## 364            Vietnam      Asia 1967    637.1233
## 365            Vietnam      Asia 1972    699.5016
## 366            Vietnam      Asia 1977    713.5371
## 367            Vietnam      Asia 1982    707.2358
## 368            Vietnam      Asia 1987    820.7994
## 369            Vietnam      Asia 1992    989.0231
## 370            Vietnam      Asia 1997   1385.8968
## 371            Vietnam      Asia 2002   1764.4567
## 372            Vietnam      Asia 2007   2441.5764
## 373 West Bank and Gaza      Asia 1952   1515.5923
## 374 West Bank and Gaza      Asia 1957   1827.0677
## 375 West Bank and Gaza      Asia 1962   2198.9563
## 376 West Bank and Gaza      Asia 1967   2649.7150
## 377 West Bank and Gaza      Asia 1972   3133.4093
## 378 West Bank and Gaza      Asia 1977   3682.8315
## 379 West Bank and Gaza      Asia 1982   4336.0321
## 380 West Bank and Gaza      Asia 1987   5107.1974
## 381 West Bank and Gaza      Asia 1992   6017.6548
## 382 West Bank and Gaza      Asia 1997   7110.6676
## 383 West Bank and Gaza      Asia 2002   4515.4876
## 384 West Bank and Gaza      Asia 2007   3025.3498
## 385        Yemen, Rep.      Asia 1952    781.7176
## 386        Yemen, Rep.      Asia 1957    804.8305
## 387        Yemen, Rep.      Asia 1962    825.6232
## 388        Yemen, Rep.      Asia 1967    862.4421
## 389        Yemen, Rep.      Asia 1972   1265.0470
## 390        Yemen, Rep.      Asia 1977   1829.7652
## 391        Yemen, Rep.      Asia 1982   1977.5570
## 392        Yemen, Rep.      Asia 1987   1971.7415
## 393        Yemen, Rep.      Asia 1992   1879.4967
## 394        Yemen, Rep.      Asia 1997   2117.4845
## 395        Yemen, Rep.      Asia 2002   2234.8208
## 396        Yemen, Rep.      Asia 2007   2280.7699

From the results above, you can see how only data from Asia were selected.