-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft.R
executable file
·151 lines (116 loc) · 3.99 KB
/
draft.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#! /bin/Rscript
library(tidyverse)
library(rvest)
library(httr)
link1 <- "https://vnexpress.net/microservice/sheet/type/covid19_2021_by_map"
link2 <- "https://vnexpress.net/microservice/sheet/type/covid19_2021_by_location"
link3 <- "https://vnexpress.net/microservice/sheet/type/covid19_2021_by_total"
link4 <- "https://vnexpress.net/microservice/sheet/type/covid19_2021_by_day"
link <- tibble(url = c(link1, link2, link3, link4))
get_data <- function(link) {
GET(link) %>%
read_html() %>%
html_text() %>%
read_csv()
}
result <- link %>%
mutate(data= map(url, get_data))
df2 <- result[1, ]$data[[1]] %>%
janitor::clean_names()
df2 <- df2 %>%
select(name = english)
# main-plot (map) data ----------------------------------------------------------------------------------
# load data to google sheet
library(googlesheets4)
gs4_auth(email = '[email protected]')
ss <- "1NL6ikAYrvB2law5ZqdF3gTURMfONIgxdDbu88bvr36k"
df1 <- result[2,]$data[[1]]
df1 <- df1 %>%
select(1:63) %>%
rename(date =1) %>%
filter(!is.na(date)) %>%
pivot_longer(-date) %>%
mutate(name = haitools::str_remove_accent(name)) %>%
replace_na(list(value =0))
df1 <- df1 %>%
mutate(date = paste0(date, "/2021"),
date = lubridate::dmy(date)) %>%
group_by(name) %>%
arrange(date) %>%
mutate(total = cumsum(value)) %>%
ungroup()
df1 %>%
mutate(name = case_when(name == "TP HCM" ~ "Ho Chi Minh",
str_detect(name, "Ba Ria")~ "Ba Ria - Vung Tau",
TRUE ~ name)) %>%
group_by(name) %>%
slice(1: (n()-1)) %>%
mutate(moving_avg = slider::slide_dbl(value ,~ mean(.x),.before =7)) %>%
ungroup() %>%
write_sheet(ss, sheet = "main-plot")
# case-in-community and population data -----------------------------------------------------------------
result$data[[4]] |>
janitor::clean_names() |>
select(date = ngay, outside = cong_dong, blockade, community) |>
filter(!is.na(outside)) |>
replace_na(list(blockade = 1, community = 1)) %>%
slice_head(n = nrow(.)-1) |>
mutate(date = paste0(date,'/2021'),
date =lubridate::dmy(date)) |>
pivot_longer(-date) |>
write_sheet(ss, "case_in_community")
result$data[[1]] |>
janitor::clean_names() |>
select(tinh_thanh) |>
mutate(tinh_thanh = stringi::stri_trans_general(tinh_thanh, id = "Latin - ASCII"),
tinh_thanh = if_else(tinh_thanh == "TP. Ho Chi Minh", "Ho Chi Minh", tinh_thanh)) |>
write_sheet(ss, "danso")
# read medical statistic --------------------------------------------------------------------------------
# library(tidyverse)
# df_bs <- read_csv("~/Downloads/vn-medical-stat/so_bs.csv",skip = 1) |>
# janitor::clean_names()
#
# df_bv <- read_csv("~/Downloads/vn-medical-stat/so_cs_yte.csv") |>
# janitor::clean_names()
#
# df_gbenh <- read_csv("~/Downloads/vn-medical-stat/so_giuong_benh.csv",skip = 1) |>
# janitor::clean_names()
# remove_province <- c(
# "WHOLE COUNTRY",
# "Mekong River Delta" ,
# "South East",
# "Red River Delta",
# "Northern midlands and mountain areas",
# "Northern Central area and Central coastal area",
# "Central Highlands",
# "Ha Tay"
# )
#
# cleaner <- . %>%
# pivot_longer(cols = -cities_provincies) %>%
# filter(! cities_provincies %in% remove_province ) %>%
# mutate(name = str_remove(name, "x2017_")) %>%
# filter( name != 'total')
#
#
#
# df_bv %>%
# cleaner() %>%
# mutate(category = "# Hospitals") %>%
# bind_rows(
# df_bs %>%
# cleaner() %>%
# mutate(category = 'medical staff')
# ) %>%
# bind_rows(
# df_gbenh %>%
# cleaner() %>%
# mutate(category = "# bed")
# ) %>%
# rename(Province = cities_provincies) %>%
# mutate(Province = case_when(str_detect(Province,"Ho Chi Minh") ~ "Ho Chi Minh",
# Province == "Thua Thien-Hue"~ 'Thua Thien Hue',
# TRUE ~ Province),
# Province = str_replace_all(Province, "\\s+", " ")) %>%
# write_sheet(ss, "medical-stat")
#