-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_coronadata.R
93 lines (77 loc) · 2.77 KB
/
get_coronadata.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
get_coronadata <- function(show.na = FALSE) {
if(!"dplyr" %in% rownames(installed.packages())) {
install.packages("dplyr")
}
if(!"purrr" %in% rownames(installed.packages())) {
install.packages("purrr")
}
if(!"tibble" %in% rownames(installed.packages())) {
install.packages("tibble")
}
if(!"tidyr" %in% rownames(installed.packages())) {
install.packages("tidyr")
}
if(!"stringr" %in% rownames(installed.packages())) {
install.packages("stringr")
}
if(!"magrittr" %in% rownames(installed.packages())) {
install.packages("magrittr")
}
if(!"rvest" %in% rownames(installed.packages())) {
install.packages("rvest")
}
if(!"xml2" %in% rownames(installed.packages())) {
install.packages("xml2")
}
if(!"lubridate" %in% rownames(installed.packages())) {
install.packages("lubridate")
}
if (!"data.table" %in% rownames(installed.packages())) {
install.packages("data.table")
}
`%>%` <- magrittr::`%>%`
# Extract data
freq_coronavirus <- xml2::read_html("https://www.worldometers.info/coronavirus/") %>%
rvest::html_nodes("table") %>%
rvest::html_table() %>%
purrr::map(~ .x %>%
dplyr::mutate(NewCases = as.character(NewCases),
NewDeaths = as.character(NewDeaths))) %>%
data.table::rbindlist(idcol = "Date_extract") %>%
tibble::as_tibble() %>%
dplyr::select(-`#`) %>%
dplyr::filter(`Country,Other` != "")
# Format date
freq_coronavirus <- freq_coronavirus %>%
dplyr::mutate(
Date_extract = dplyr::case_when(
Date_extract == 1 ~ lubridate::ymd(Sys.Date()),
Date_extract == 2 ~ lubridate::ymd(Sys.Date()) - 1,
TRUE ~ lubridate::ymd(Sys.Date()) - 2
)
# `Reported1st case` = paste0("2020 ", `Reported1st case`, sep = ""),
# `Reported1st case` = lubridate::ymd(`Reported1st case`)
)
# Format numbers
freq_coronavirus <- freq_coronavirus %>%
dplyr::mutate_at(dplyr::vars(TotalCases:`Tests/1M pop`),
~ stringr::str_remove(., "[+]")) %>%
dplyr::mutate_at(dplyr::vars(TotalCases:`1 Testevery X ppl`),
~ stringr::str_replace_all(., ",", ""))
suppressWarnings(
freq_coronavirus <- freq_coronavirus %>%
dplyr::mutate_at(dplyr::vars(-c(Date_extract, `Country,Other`,
Continent)),
as.numeric)
)
# Add NA or not
if(show.na == FALSE) {
freq_coronavirus <- freq_coronavirus %>%
dplyr::mutate_at(dplyr::vars(-c(Date_extract, `Country,Other`,
Continent)),
~ replace(., is.na(.), 0))
return(freq_coronavirus)
} else {
return(freq_coronavirus)
}
}