Q 3.1:Fetch data from the web

library(XML)
url <- "http://wdi.worldbank.org/table/4.1"
table.raw <- readHTMLTable(url)[[3]]
table_3 <- readHTMLTable(url)[[3]]
table_industry <- table_3[, c(1, 6, 7)]
names(table_industry)[1] <- "country.names"
names(table_industry)[2] <- "avganngrwth%(90-00)"
names(table_industry)[3] <- "avganngrwth%(00-12)"
table_industry
##                      country.names avganngrwth%(90-00) avganngrwth%(00-12)
## 1                      Afghanistan                  ..                 9.4
## 2                          Albania                -0.5                 4.6
## 3                          Algeria                 1.8                 3.3
## 4                   American Samoa                  ..                  ..
## 5                          Andorra                  ..                  ..
## 6                           Angola                 4.4                11.2
## 7              Antigua and Barbuda                 4.7                 3.1
## 8                        Argentina                 3.8                 5.9
## 9                          Armenia                -7.4                 6.9
## 10                           Aruba                  ..                  ..
## 11                       Australia                 2.8                 2.8
## 12                         Austria                 2.5                 1.6
## 13                      Azerbaijan                -2.1                18.9
## 14                    Bahamas, The                 3.9                 2.7
## 15                         Bahrain                  ..                  ..
## 16                      Bangladesh                 7.3                 7.7
## 17                        Barbados                 1.4                -0.9
## 18                         Belarus                -1.9                10.8
## 19                         Belgium                 1.8                 0.6
## 20                          Belize                 2.8                 5.3
## 21                           Benin                 3.9                 3.2
## 22                         Bermuda                  ..                -1.5
## 23                          Bhutan                 6.6                10.8
## 24                         Bolivia                 4.1                 5.4
## 25          Bosnia and Herzegovina                22.8                 5.3
## 26                        Botswana                 2.6                -0.2
## 27                          Brazil                 2.4                 2.8
## 28               Brunei Darussalam                 2.0                -0.9
## 29                        Bulgaria               -19.5                 4.2
## 30                    Burkina Faso                 5.9                 5.1
## 31                         Burundi                -5.2                 1.1
## 32                      Cabo Verde                 9.0                 7.1
## 33                        Cambodia                14.3                10.0
## 34                        Cameroon                -0.9                 0.0
## 35                          Canada                 3.2                -0.1
## 36                  Cayman Islands                  ..                  ..
## 37        Central African Republic                 0.1                 4.1
## 38                            Chad                  ..                10.0
## 39                 Channel Islands                  ..                  ..
## 40                           Chile                 4.5                 4.3
## 41                           China                13.7                11.6
## 42            Hong Kong SAR, China                  ..                -0.8
## 43                Macao SAR, China                  ..                  ..
## 44                        Colombia                 1.5                 4.6
## 45                         Comoros                 3.9                 2.8
## 46                Congo, Dem. Rep.                -8.7                 8.5
## 47                     Congo, Rep.                 4.5                 3.3
## 48                      Costa Rica                 6.2                 4.4
## 49                   Cote d'Ivoire                 6.3                -0.7
## 50                         Croatia                 3.7                 1.4
## 51                            Cuba                -0.9                 2.8
## 52                         Curacao                  ..                  ..
## 53                          Cyprus                 0.6                 3.0
## 54                  Czech Republic                -0.4                 5.2
## 55                         Denmark                 2.3                -1.3
## 56                        Djibouti                -5.2                 4.9
## 57                        Dominica                 2.2                 3.9
## 58              Dominican Republic                 7.1                 2.8
## 59                         Ecuador                 1.5                 4.6
## 60                Egypt, Arab Rep.                 5.1                 5.2
## 61                     El Salvador                 5.3                 1.4
## 62               Equatorial Guinea                  ..                12.3
## 63                         Eritrea                15.0                 0.3
## 64                         Estonia                 6.5                 4.3
## 65                        Ethiopia                 4.2                10.1
## 66                  Faeroe Islands                  ..                  ..
## 67                            Fiji                 4.0                 0.5
## 68                         Finland                 3.4                 1.5
## 69                          France                 1.0                 0.0
## 70                French Polynesia                  ..                  ..
## 71                           Gabon                 1.6                 1.5
## 72                     Gambia, The                 1.6                 3.1
## 73                         Georgia                -8.1                 7.9
## 74                         Germany                -0.1                 1.1
## 75                           Ghana                  ..                  ..
## 76                          Greece                  ..                -6.9
## 77                       Greenland                  ..                  ..
## 78                         Grenada                 5.8                -0.5
## 79                            Guam                  ..                  ..
## 80                       Guatemala                 4.3                 2.4
## 81                          Guinea                 4.7                 2.9
## 82                   Guinea-Bissau                  ..                  ..
## 83                          Guyana                 8.2                 2.5
## 84                           Haiti                  ..                  ..
## 85                        Honduras                 3.6                 3.3
## 86                         Hungary                 3.7                 2.1
## 87                         Iceland                 1.8                 2.0
## 88                           India                 6.1                 8.2
## 89                       Indonesia                 5.2                 4.3
## 90              Iran, Islamic Rep.                 2.6                 6.9
## 91                            Iraq                  ..                 3.1
## 92                         Ireland                11.5                 3.7
## 93                     Isle of Man                  ..                  ..
## 94                          Israel                  ..                  ..
## 95                           Italy                 1.0                -0.7
## 96                         Jamaica                -0.8                -1.1
## 97                           Japan                -0.4                 0.5
## 98                          Jordan                 5.2                 6.7
## 99                      Kazakhstan                -8.6                 7.9
## 100                          Kenya                 1.2                 4.6
## 101                       Kiribati                 6.9                -1.8
## 102               Korea, Dem. Rep.                  ..                  ..
## 103                    Korea, Rep.                 6.0                 5.2
## 104                         Kosovo                  ..                  ..
## 105                         Kuwait                  ..                  ..
## 106                Kyrgyz Republic               -10.3                 1.2
## 107                        Lao PDR                11.1                13.4
## 108                         Latvia                -8.3                 3.7
## 109                        Lebanon                -0.2                 5.3
## 110                        Lesotho                 7.4                 5.4
## 111                        Liberia                  ..                17.7
## 112                          Libya                  ..                  ..
## 113                  Liechtenstein                  ..                  ..
## 114                      Lithuania                 3.3                 5.8
## 115                     Luxembourg                 3.6                -1.0
## 116                 Macedonia, FYR                -2.3                 3.1
## 117                     Madagascar                 2.4                 4.2
## 118                         Malawi                 2.0                 6.1
## 119                       Malaysia                 7.8                 3.0
## 120                       Maldives                  ..                 8.5
## 121                           Mali                 6.4                 3.6
## 122                          Malta                 3.3                -1.8
## 123               Marshall Islands                  ..                  ..
## 124                     Mauritania                 4.6                 6.2
## 125                      Mauritius                 5.4                 2.1
## 126                         Mexico                 3.6                 1.3
## 127          Micronesia, Fed. Sts.                -0.6                -0.8
## 128                        Moldova               -13.6                -1.1
## 129                         Monaco                  ..                  ..
## 130                       Mongolia                 1.5                 5.7
## 131                     Montenegro                  ..                 4.5
## 132                        Morocco                 3.0                 3.9
## 133                     Mozambique                12.3                 7.8
## 134                        Myanmar                  ..                  ..
## 135                        Namibia                 2.4                 4.8
## 136                          Nepal                 7.1                 2.7
## 137                    Netherlands                 1.7                 0.7
## 138                  New Caledonia                  ..                  ..
## 139                    New Zealand                 2.5                 1.1
## 140                      Nicaragua                 5.3                 3.3
## 141                          Niger                  ..                  ..
## 142                        Nigeria                 0.9                 3.6
## 143       Northern Mariana Islands                  ..                  ..
## 144                         Norway                 4.0                -0.6
## 145                           Oman                  ..                  ..
## 146                       Pakistan                 4.1                 5.9
## 147                          Palau                  ..                -4.7
## 148                         Panama                 5.8                 7.0
## 149               Papua New Guinea                  ..                  ..
## 150                       Paraguay                 3.3                 2.0
## 151                           Peru                 5.4                 6.5
## 152                    Philippines                 3.2                 4.3
## 153                         Poland                 6.7                 5.8
## 154                       Portugal                 3.2                -1.0
## 155                    Puerto Rico                  ..                  ..
## 156                          Qatar                  ..                  ..
## 157                        Romania                -1.2                 5.1
## 158             Russian Federation                -7.1                 3.5
## 159                         Rwanda                -3.8                 9.7
## 160                          Samoa                 0.5                 2.2
## 161                     San Marino                  ..                  ..
## 162          Sao Tome and Principe                  ..                  ..
## 163                   Saudi Arabia                 2.2                 4.9
## 164                        Senegal                 3.8                 3.5
## 165                         Serbia                  ..                 2.0
## 166                     Seychelles                11.6                 2.7
## 167                   Sierra Leone                -3.3                 5.6
## 168                      Singapore                 7.7                 6.1
## 169      Sint Maarten (Dutch part)                  ..                  ..
## 170                Slovak Republic                 4.1                 7.9
## 171                       Slovenia                 1.6                 3.3
## 172                Solomon Islands                 5.3                 0.2
## 173                        Somalia                  ..                  ..
## 174                   South Africa                 1.0                 2.4
## 175                    South Sudan                  ..                  ..
## 176                          Spain                 1.9                -0.2
## 177                      Sri Lanka                 6.9                 6.3
## 178            St. Kitts and Nevis                 5.7                -1.5
## 179                      St. Lucia                 3.3                 2.0
## 180       St. Martin (French part)                  ..                  ..
## 181 St. Vincent and the Grenadines                 2.8                 2.2
## 182                          Sudan                 8.9                 7.5
## 183                       Suriname                -1.5                 6.4
## 184                      Swaziland                 3.1                 0.7
## 185                         Sweden                 4.1                 2.1
## 186                    Switzerland                 0.1                 2.1
## 187           Syrian Arab Republic                  ..                  ..
## 188                     Tajikistan               -11.4                 1.4
## 189                       Tanzania                 3.1                 9.0
## 190                       Thailand                 5.7                 4.8
## 191                    Timor-Leste                  ..                 9.6
## 192                           Togo                 1.8                 8.3
## 193                          Tonga                 2.6                 2.2
## 194            Trinidad and Tobago                 3.2                 6.6
## 195                        Tunisia                 4.4                 3.0
## 196                         Turkey                 4.7                 5.1
## 197                   Turkmenistan                -2.7                20.2
## 198       Turks and Caicos Islands                  ..                  ..
## 199                         Tuvalu                 3.1                 4.6
## 200                         Uganda                12.3                 8.8
## 201                        Ukraine               -12.6                 2.8
## 202           United Arab Emirates                  ..                 3.3
## 203                 United Kingdom                 2.5                -0.9
## 204                  United States                 3.7                 0.2
## 205                        Uruguay                 1.3                 3.2
## 206                     Uzbekistan                -3.4                 4.7
## 207                        Vanuatu                 1.3                 4.1
## 208                  Venezuela, RB                 1.2                 2.2
## 209                        Vietnam                11.9                 7.3
## 210          Virgin Islands (U.S.)                  ..                  ..
## 211             West Bank and Gaza                  ..                  ..
## 212                    Yemen, Rep.                 5.2                 2.0
## 213                         Zambia                -4.2                 8.3
## 214                       Zimbabwe                 1.6                -1.4
## 215                          World                 2.4                 2.6
## 216                     Low income                 3.2                 6.7
## 217                  Middle income                 4.8                 6.9
## 218            Lower middle income                 3.2                 5.7
## 219            Upper middle income                 5.2                 7.2
## 220            Low & middle income                 4.8                 6.9
## 221            East Asia & Pacific                10.7                 9.9
## 222          Europe & Central Asia                -2.4                 5.4
## 223      Latin America & Caribbean                 2.9                 2.7
## 224     Middle East & North Africa                 2.9                 3.7
## 225                     South Asia                 6.0                 7.9
## 226             Sub-Saharan Africa                 1.7                 4.1
## 227                    High income                 1.8                 1.0
## 228                      Euro area                 1.0                 0.4

avg_table <- function(name) {
    return(subset(table_industry, country.names == name)[3])
}

When the function avg_table() is called with the test value “Tonga” (a country), it returns:

avg_table("Tonga")
##     avganngrwth%(00-12)
## 193                 2.2