-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_district_counts.R
49 lines (38 loc) · 1.66 KB
/
get_district_counts.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
#' Pull covid19india district-level data
#' @param path The URL path for the data. Default: `https://api.covid19india.org/csv/latest/districts.csv`
#' @param raw Pull raw unaltered data. Default is `FALSE`
#' @return Pulls the district-level time-series case, death, and recovered data directly from covid19india.org.
#' @import data.table
#' @importFrom janitor clean_names
#' @export
#' @examples
#' \dontrun{
#' get_district_counts()
#' }
get_district_counts <- function(
path = "https://api.covid19india.org/csv/latest/districts.csv",
raw = FALSE
) {
d <- data.table::fread(path, showProgress = FALSE)
if (raw == FALSE) {
setnames(d, names(d), janitor::make_clean_names(names(d)))
setnames(d, c("confirmed", "recovered", "deceased"), c("total_cases", "total_recovered", "total_deaths"))
d <- data.table::melt(d[, !c("other")], id.vars = c("date", "state", "district"))[value >= 0]
d <- data.table::dcast.data.table(d, date + state + district ~ variable)[order(date)][
, `:=` (
daily_cases = total_cases - data.table::shift(total_cases),
daily_recovered = total_recovered - data.table::shift(total_recovered),
daily_deaths = total_deaths - data.table::shift(total_deaths)
), by = c("state", "district")
][!is.na(daily_cases) & !is.na(daily_recovered) & !is.na(daily_deaths)][
, date := as.Date(date)
][
, .(state, district, date,
daily_cases, daily_recovered, daily_deaths,
total_cases, total_recovered, total_deaths)
]
d <- na.omit(d, c("daily_cases", "daily_recovered", "daily_deaths"))
setkeyv(d, cols = c("state", "district", "date"))
}
return(d)
}