Loading the libraries

library(httr)
library(rvest)

TASK 1

# Get a COVID-19 pandemic Wiki page using HTTP request
get_wiki_covid19_page <- function() {
  # Wiki page base
  wiki_url <-  "https://en.wikipedia.org/w/index.php?title=Template:COVID-19_testing_by_country"
  
  
  COVID19 <- list(title="Template:COVID-19_testing_by_country")
  # - Use the `GET` function in httr library with a `url` argument and a `query` arugment to get a HTTP response
  Http_response <- GET(url = wiki_url, query = COVID19)
  # Use the `return` function to return the response
  return(Http_response ) 
}
# Call the get_wiki_covid19_page function and print the response
response <- get_wiki_covid19_page()

TASK 2

Extract COVID-19 testing data table from the wiki HTML page

# Get the root html node from the http response in task 1 
root_node <- read_html(response)
# Get the table node from the root html node
table_node <- html_nodes(root_node, "table")
# Read the table node and convert it into a data frame, and print the data frame for review
COVID_dframe <- as.data.frame(html_table(table_node[2]))
head(COVID_dframe)
##     Country.or.region     Date.a.  Tested Units.b. Confirmed.cases.
## 1         Afghanistan 17 Dec 2020 154,767  samples           49,621
## 2             Albania 18 Feb 2021 428,654  samples           96,838
## 3             Algeria  2 Nov 2020 230,553  samples           58,574
## 4             Andorra 23 Feb 2022 300,307  samples           37,958
## 5              Angola  2 Feb 2021 399,228  samples           20,981
## 6 Antigua and Barbuda  6 Mar 2021  15,268  samples              832
##   Confirmed..tested.. Tested..population.. Confirmed..population..   Ref.
## 1                32.1                 0.40                    0.13    [1]
## 2                22.6                 15.0                     3.4    [2]
## 3                25.4                 0.53                    0.13 [3][4]
## 4                12.6                  387                    49.0    [5]
## 5                 5.3                  1.3                   0.067    [6]
## 6                 5.4                 15.9                    0.86    [7]

TASK 3: Pre-process and export the extracted data frame

Print the summary and tail of the data frame

summary(COVID_dframe)
##  Country.or.region    Date.a.             Tested            Units.b.        
##  Length:173         Length:173         Length:173         Length:173        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##  Confirmed.cases.   Confirmed..tested.. Tested..population..
##  Length:173         Length:173          Length:173          
##  Class :character   Class :character    Class :character    
##  Mode  :character   Mode  :character    Mode  :character    
##  Confirmed..population..     Ref.          
##  Length:173              Length:173        
##  Class :character        Class :character  
##  Mode  :character        Mode  :character
tail(COVID_dframe)
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        Country.or.region
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Uzbekistan
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            Venezuela
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              Vietnam
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               Zambia
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Zimbabwe
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Date.a.
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           7 Sep 2020
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          30 Mar 2021
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          28 Aug 2022
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          10 Mar 2022
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          15 Oct 2022
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   Tested
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            2,630,000
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            3,179,074
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           45,772,571
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            3,301,860
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            2,529,087
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Units.b.
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              samples
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              samples
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              samples
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              samples
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              samples
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         Confirmed.cases.
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               43,975
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              159,149
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           11,403,302
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              314,850
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              257,893
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Confirmed..tested..
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1.7
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  5.0
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 24.9
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  9.5
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 10.2
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     Tested..population..
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  7.7
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 11.0
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 46.4
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 19.0
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 17.0
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Confirmed..population..
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 0.13
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 0.55
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 11.6
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1.8
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  1.7
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     Ref.
## 168                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                [187]
## 169                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                [188]
## 170                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                [189]
## 171                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                [190]
## 172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             [3][191]
## 173 .mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}\n^ Local time.\n\n^ For some countries it is unclear whether they report samples or cases. One person tested twice is recorded as one case and two samples.\n\n^ Excluding Taiwan.\n\n^ Excluding Northern Cyprus.\n\n^ Excluding Greenland and the Faroe Islands.\n\n^ Excluding Overseas France.\n\n^ Testing data from 4 May to 12 May is missing because of the transition to the new reporting system SI-DEP.\n\n^ Excluding Abkhazia and South Ossetia.\n\n^ Data for residents only.\n\n^ Excluding Transnistria.\n\n^ Northern Cyprus is not recognized as a sovereign state by any country except Turkey.\n\n^ Includes data for Liechtenstein.\n\n^ Not a United Nations member.
#pre-process the data
preprocess_COVID_dframe <- function(COVID_dframe){
  #Remove the world row
  #Covid_dframe<- COVID_dframe[!(COVID_dframe$Country.or.region=="World"),]
  #Remove last row
  Covid_dframe<- COVID_dframe[1:172,]
  #We don't need the units and ref columns, so can be removed
  Covid_dframe["Ref."] <- NULL
  Covid_dframe["Units.b."] <- NULL
  #Renaming the columns
  names(Covid_dframe) <- c("country","date","tested","confirmed","confirmed.tested.ratio","tested.population.ratio","confirmed.population.ratio")
  #Convert column data types
  Covid_dframe$country <- as.factor(Covid_dframe$country)
  Covid_dframe$date <- as.factor(Covid_dframe$date)
  Covid_dframe$tested <- as.numeric(gsub(",","",Covid_dframe$tested))
  Covid_dframe$confirmed <- as.numeric(gsub(",","",Covid_dframe$confirmed))
  Covid_dframe$confirmed.tested.ratio <- as.numeric(gsub(",","",Covid_dframe$confirmed.tested.ratio))
  Covid_dframe$tested.population.ratio <- as.numeric(gsub(",","",Covid_dframe$tested.population.ratio))
  Covid_dframe$confirmed.population.ratio <- as.numeric(gsub(",","",Covid_dframe$confirmed.population.ratio))
  
}
Covid_dframe <- preprocess_COVID_dframe(COVID_dframe)
#Export the data frame to a csv file
write.table(Covid_dframe, "Shalom_Covid_Project.csv")

# Get working directory
Getwd <- getwd()
# Get exported
file_path <- paste(Getwd, sep="", "/Shalom_Covid_Project.csv")
# File path
print(file_path)
## [1] "C:/Users/user/Documents/Shalom_Covid_Project.csv"
file.exists(file_path)
## [1] TRUE

TASK 4

Read covid_data_frame_csv from the csv file (using our processed data frame)

covid_data_frame_csv <- read.table("C:/Users/user/Documents/ShalomProject.csv",header = TRUE, sep=" ")
head(covid_data_frame_csv) 
##               Country        Date Tested Confirmed Confirmed.tested.ratio
## 1         Afghanistan 17 Dec 2020 154767     49621                   32.1
## 2             Albania 18 Feb 2021 428654     96838                   22.6
## 3             Algeria  2 Nov 2020 230553     58574                   25.4
## 4             Andorra 23 Feb 2022 300307     37958                   12.6
## 5              Angola  2 Feb 2021 399228     20981                    5.3
## 6 Antigua and Barbuda  6 Mar 2021  15268       832                    5.4
##   Tested.population.ratio Confirmed.population.ratio
## 1                    0.40                      0.130
## 2                   15.00                      3.400
## 3                    0.53                      0.130
## 4                  387.00                     49.000
## 5                    1.30                      0.067
## 6                   15.90                      0.860

TASK 5

Calculate worldwide COVID testing positive ratio

#Get the total confirmed cases worldwide
Totalconfirmed <- sum(covid_data_frame_csv$Confirmed)
Totalconfirmed
## [1] 431434555
#Get the total tested cases worldwide
Totaltested <- sum(covid_data_frame_csv$Tested)
Totaltested
## [1] 5396881644
# Get the positive ratio (confirmed/tested)
Confirmed.tested.ratio <- Totalconfirmed/Totaltested
Confirmed.tested.ratio 
## [1] 0.07994145

TASK 6

Get a country list which reported their testing data

# Get the `country` column
head(covid_data_frame_csv$Country)
## [1] "Afghanistan"         "Albania"             "Algeria"            
## [4] "Andorra"             "Angola"              "Antigua and Barbuda"
tail(covid_data_frame_csv$Country)
## [1] "Uruguay"    "Uzbekistan" "Venezuela"  "Vietnam"    "Zambia"    
## [6] "Zimbabwe"
# Convert the country column into character so that you can easily sort them
class(covid_data_frame_csv$Country)
## [1] "character"
# Sort the countries AtoZ
AtoZ <- sort(covid_data_frame_csv$Country,decreasing=FALSE)

# Print the sorted ZtoA list
head(AtoZ)
## [1] "Afghanistan"         "Albania"             "Algeria"            
## [4] "Andorra"             "Angola"              "Antigua and Barbuda"
tail(AtoZ)
## [1] "Uruguay"    "Uzbekistan" "Venezuela"  "Vietnam"    "Zambia"    
## [6] "Zimbabwe"
# Sort the countries ZtoA
ZtoA <- sort(covid_data_frame_csv$Country,decreasing=TRUE)

# Print the sorted ZtoA list
head(ZtoA)
## [1] "Zimbabwe"   "Zambia"     "Vietnam"    "Venezuela"  "Uzbekistan"
## [6] "Uruguay"
tail(ZtoA)
## [1] "Antigua and Barbuda" "Angola"              "Andorra"            
## [4] "Algeria"             "Albania"             "Afghanistan"

TASK 7

Identify countries names with a specific pattern # Use a regular expression United.+ to find matches # Print the matched country names

matches <- regexpr("United",covid_data_frame_csv$Country)
regmatches(covid_data_frame_csv$Country,matches)
## [1] "United" "United" "United"

TASK 8

Pick two countries you are interested, and then review their testing data Select a subset (should be only one row) of data frame based on a selected country name and columns

Country1 <- covid_data_frame_csv[118,c(1,2,3,5,7)]
Country1
##     Country        Date  Tested Confirmed.tested.ratio
## 118 Nigeria 28 Feb 2021 1544008                   10.1
##     Confirmed.population.ratio
## 118                      0.076
Country2 <- covid_data_frame_csv[101,c(1,2,3,5,7)]
Country2
##     Country       Date Tested Confirmed.tested.ratio Confirmed.population.ratio
## 101    Mali 7 Jul 2021 322504                    4.5                      0.071

TASK 9

Compare which one of the selected countries has a larger ratio of confirmed cases to population

if(Country1[5]>Country2[5]){
  print("Nigeria is greater")
} else {
  print("Mali is greater")
}
## [1] "Nigeria is greater"

TASK 10

Find countries with confirmed to population ratio rate less than a threshold

Threshold <- 1.5
covid_data_frame_csv[covid_data_frame_csv$Confirmed.population.ratio<Threshold,"Country"]
##  [1] "Afghanistan"         "Algeria"             "Angola"             
##  [4] "Antigua and Barbuda" "Bangladesh"          "Benin"              
##  [7] "Brunei"              "Burkina Faso"        "Burundi"            
## [10] "Cambodia"            "Cameroon"            "Chad"               
## [13] "China[c]"            "DR Congo"            "Egypt"              
## [16] "Equatorial Guinea"   "Ethiopia"            "Gabon"              
## [19] "Gambia"              "Ghana"               "Grenada"            
## [22] "Guinea"              "Guinea-Bissau"       "Haiti"              
## [25] "Ivory Coast"         "Japan"               "Kenya"              
## [28] "Kyrgyzstan"          "Laos"                "Liberia"            
## [31] "Madagascar"          "Malawi"              "Mali"               
## [34] "Mauritania"          "Mauritius"           "Mozambique"         
## [37] "Myanmar"             "New Caledonia"       "Niger"              
## [40] "Nigeria"             "North Korea"         "Pakistan"           
## [43] "Papua New Guinea"    "Rwanda"              "Senegal"            
## [46] "Singapore"           "South Korea"         "South Sudan"        
## [49] "Sri Lanka"           "Sudan"               "Tanzania"           
## [52] "Thailand"            "Togo"                "Uganda"             
## [55] "Uzbekistan"          "Venezuela"