Site icon R-bloggers

Web Scraping worldometers for Coronavirus

[This article was first published on R – Predictive Hacks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

One of the most popular web pages about Covid-19 is the worldometers which provides a detailed report about Coronavirus cases by country. Today, we will show how we can use R to Web Scrape the summary table of the site.

library(tidyverse)
library(rvest)

url <- "https://www.worldometers.info/coronavirus/"

my_table<-url%>%read_html()%>%html_table()%>%.[[1]]

# There are some "+" symbols and the "," 
# for the thousand separators that we wan to remove them
my_table[]<-lapply(my_table, function(x) (gsub("\\,|\\+", "", (x))))

# convert all but the first and last column to numeric

my_table[,c(2:12)] <- sapply(my_table[c(2:12)],as.numeric)

Since we got the data and we cleaned them, we can provide some statistics like:

Q: Which are the top 10 countries in Deaths per 1M Population?

my_table%>%arrange(-`Deaths/1M pop`)%>%
          select(`Country,Other`,`Deaths/1M pop`)%>%
          head(10)
 

   Country,Other Deaths/1M pop
1     San Marino          1032
2        Andorra           375
3          Spain           363
4          Italy           322
5        Belgium           311
6         France           212
7   Sint Maarten           210
8    Netherlands           160
9             UK           156
10   Switzerland           126

Enjoy your analysis!

To leave a comment for the author, please follow the link and comment on their blog: R – Predictive Hacks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.