Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocess.R 11 KB

You have to be logged in to leave a comment. Sign In
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  1. library(readr)
  2. library(dplyr)
  3. library(tidyr)
  4. library(tidyselect)
  5. library(lubridate)
  6. # Reading raw data --------------------------------------------------------
  7. # Google Mobility Report (GMR) dataset
  8. raw_dataset <- read_csv(file = 'data/raw/Global_Mobility_Report.csv',
  9. col_types = paste(c(rep('c', 4),
  10. 'D',
  11. rep('d', 6)),
  12. collapse=''))
  13. colnames(raw_dataset) <- c('country_code', 'country_name', 'region_name',
  14. 'county_name', 'date', 'retail_recreation',
  15. 'grocery_pharmacy', 'parks', 'transit_stations',
  16. 'workplaces', 'residential')
  17. # COVID19 dataset from ECDC
  18. covid <- read_delim(file = 'data/raw/COVID19_worldwide_raw.csv', na = '',
  19. col_types = cols('c', 'i', 'i', 'i', 'i', 'i', 'c', 'c',
  20. 'c', 'i'),
  21. delim = ',')
  22. colnames(covid) <- c('date', 'day', 'month', 'year', 'new_cases', 'new_deaths',
  23. 'country_name', 'country_id', 'territory_id',
  24. 'pop_data_2018')
  25. # Country details from UN Data
  26. country_details <- read_delim(file = 'data/raw/UN_dataset.tsv', delim = '\t',
  27. col_types = paste(c('c',
  28. rep('d', 173),
  29. 'c'),
  30. collapse=''))
  31. # Preprocessing step ---------------------------------------------------------
  32. ####
  33. #
  34. # Working on GMR dataset
  35. #
  36. ####
  37. # Create a long table from the wide original version
  38. preprocessed_dataset <- pivot_longer(raw_dataset, cols=6:11, names_to = 'plot_name',
  39. values_to = 'variation')
  40. rm(raw_dataset)
  41. ####
  42. #
  43. # Working on COVID19 dataset
  44. #
  45. ####
  46. covid %>%
  47. # These columns are useless
  48. select(-c('day', 'month', 'year', 'country_id', 'territory_id')) %>%
  49. # Convert date to Date
  50. mutate(date, date = dmy(covid$date)) %>%
  51. # Replace _ by space in country names
  52. mutate(country_name,
  53. country_name = gsub('_',
  54. ' ',
  55. country_name)) -> covid
  56. # Some countries have different names between the datasets
  57. covid %>%
  58. mutate(country_name, country_name = case_when(
  59. country_name == 'United States of America' ~ 'United States',
  60. country_name == 'United Republic of Tanzania' ~ 'Tanzania',
  61. country_name == 'Guinea Bissau' ~ 'Guinea-Bissau',
  62. country_name == 'Bahamas' ~ 'The Bahamas',
  63. country_name == 'Myanmar' ~ 'Myanmar (Burma)',
  64. country_name == 'Cote dIvoire' ~ 'Côte d\'Ivoire',
  65. TRUE ~ country_name)
  66. ) -> covid
  67. # Some rows are missing like March 2nd and 3rd for Brazil. Apparently,
  68. # this happens when there is no case/death. Complete with these rows.
  69. covid %>%
  70. complete(date = seq.Date(min(date), max(date), by='day'), country_name) %>%
  71. mutate(new_cases = ifelse(is.na(new_cases), 0, new_cases)) %>%
  72. mutate(new_deaths = ifelse(is.na(new_deaths), 0, new_deaths)) %>%
  73. group_by(country_name) %>%
  74. fill(pop_data_2018, .direction = 'updown') -> covid
  75. ####
  76. #
  77. # Merge World COVID19 and GMR datasets
  78. #
  79. ####
  80. preprocessed_dataset <- left_join(preprocessed_dataset, covid,
  81. by = c('date', 'country_name'))
  82. rm(covid)
  83. ####
  84. #
  85. # Working on dataset after 1st merge
  86. #
  87. ####
  88. # Create column with accumulate cases/death
  89. preprocessed_dataset %>%
  90. group_by(country_name, plot_name, region_name) %>%
  91. arrange(date) %>%
  92. dplyr::mutate(acc_cases = cumsum(new_cases)) %>%
  93. dplyr::mutate(acc_deaths = cumsum(new_deaths)) %>%
  94. ungroup -> preprocessed_dataset
  95. # Set new_cases and new_deaths to NA for regions, because our COVID19 dataset
  96. # only has data for countries.
  97. preprocessed_dataset %>%
  98. mutate(new_cases = ifelse(!is.na(region_name),
  99. NA,
  100. new_cases)) %>%
  101. mutate(new_deaths = ifelse(!is.na(region_name),
  102. NA,
  103. new_deaths)) -> preprocessed_dataset
  104. ####
  105. #
  106. # Working on country details dataset (UN)
  107. #
  108. ####
  109. # Before merging to get more info about the countries, we must make sure all
  110. # country names are the same.
  111. #unique(preprocessed_dataset$country_name)[which(
  112. # unique(preprocessed_dataset$country_name) %in%
  113. # unique(country_details$region_name) == FALSE
  114. # )
  115. # ]
  116. country_details %>%
  117. mutate(region_name = case_when(
  118. region_name == 'Bolivia (Plurinational State of)' ~ 'Bolivia',
  119. region_name == 'Bahamas' ~ 'The Bahamas',
  120. region_name == 'Republic of Korea' ~ 'South Korea',
  121. region_name == 'Cabo Verde' ~ 'Cape Verde',
  122. region_name == 'China, Hong Kong SAR' ~ 'Hong Kong',
  123. region_name == 'Republic of Moldova' ~ 'Moldova',
  124. region_name == 'Lao People\'s Democratic Republic' ~ 'Laos',
  125. region_name == 'Myanmar' ~ 'Myanmar (Burma)',
  126. region_name == 'United Rep. of Tanzania' ~ 'Tanzania',
  127. region_name == 'United States of America' ~ 'United States',
  128. region_name == 'Venezuela (Boliv. Rep. of)' ~ 'Venezuela',
  129. region_name == 'Viet Nam' ~ 'Vietnam',
  130. TRUE ~ region_name)
  131. ) -> country_details
  132. # In some datastes US appears as United States, and in others as United States
  133. # of America. The naming was fixed earlier, but we have two rows for US. Fix.
  134. # ids <- which(country_details$region_name == 'United States')
  135. country_details[88,][,17:42] <- country_details[217,][,17:42]
  136. country_details[88,][,45:51] <- country_details[217,][,45:51]
  137. country_details[88,][,53:55] <- country_details[217,][,53:55]
  138. country_details[88,][,63:72] <- country_details[217,][,63:72]
  139. country_details[88,][,77:80] <- country_details[217,][,77:80]
  140. country_details[88,][,90:93] <- country_details[217,][,90:93]
  141. country_details[88,][,97:104] <- country_details[217,][,97:104]
  142. country_details[88,][,112:114] <- country_details[217,][,112:114]
  143. country_details[88,][,116:118] <- country_details[217,][,116:118]
  144. country_details[88,][,128:133] <- country_details[217,][,128:133]
  145. country_details[88,][,143:148] <- country_details[217,][,143:148]
  146. country_details[88,][,150] <- country_details[217,][,150]
  147. country_details[88,][,152:157] <- country_details[217,][,152:157]
  148. country_details[88,][,159:175] <- country_details[217,][,159:175]
  149. country_details <- country_details[-217, ]
  150. ####
  151. #
  152. # Merge country details and preprocessed_dataset
  153. #
  154. ####
  155. preprocessed_dataset <- left_join(preprocessed_dataset, country_details,
  156. by = c('country_name' = 'region_name'))
  157. rm(country_details)
  158. # Remove rows that are related to regions
  159. preprocessed_dataset %>%
  160. filter(is.na(region_name)) %>%
  161. select(-c('region_name', 'county_name')) -> preprocessed_dataset
  162. # There are two columns for population (2018, World Bank and 2019, UN).
  163. # Keep the most updated one.
  164. preprocessed_dataset %>%
  165. select(-c(pop_data_2018)) -> preprocessed_dataset
  166. colnames(preprocessed_dataset)[10:182] %>%
  167. # Make them all lowercase
  168. tolower %>%
  169. # Replace space by underscore
  170. gsub(' ', '_', .) %>%
  171. gsub('-', '_', .) %>%
  172. gsub('_+', '_', .) %>%
  173. gsub(',', '', .) -> colnames(preprocessed_dataset)[10:182]
  174. # Create columns for lethality rate
  175. preprocessed_dataset %>%
  176. group_by(country_name) %>%
  177. arrange(date) %>%
  178. mutate(lethality_rate_percent = (acc_deaths[n()]/acc_cases[n()])*100) %>%
  179. ungroup() -> preprocessed_dataset
  180. # Bring plot_names from row to column
  181. preprocessed_dataset %>%
  182. pivot_wider(names_from = plot_name, values_from = variation) -> preprocessed_dataset
  183. # Add n_days_since_1st_case column
  184. preprocessed_dataset %>%
  185. group_by(country_name) %>%
  186. mutate(first_case_date = min(date[acc_cases > 0])) %>%
  187. mutate(n_days_since_1st_case =
  188. if_else(acc_cases > 0,
  189. as.numeric(date - min(date[acc_cases > 0])+1),
  190. 0)) %>%
  191. ungroup() -> preprocessed_dataset
  192. # Add n_days_since_1st_death column
  193. preprocessed_dataset %>%
  194. group_by(country_name) %>%
  195. mutate(first_death_date = min(date[acc_deaths > 0])) %>%
  196. mutate(n_days_since_1st_death =
  197. if_else(acc_deaths > 0,
  198. as.numeric(date - min(date[acc_deaths > 0])+1),
  199. 0)) %>%
  200. ungroup() -> preprocessed_dataset
  201. # Set manually first case for countries whose first case happened before Feb 15
  202. # Wikipedia contributors, "2019–20 coronavirus pandemic", Wikipedia, The Free
  203. # Encyclopedia,
  204. # https://en.wikipedia.org/w/index.php?title=2019%E2%80%9320_coronavirus_pandemic&oldid=952187370
  205. # (accessed April 21, 2020).
  206. preprocessed_dataset %>%
  207. mutate(first_case_date = case_when(
  208. # country_name == 'China' ~ dmy('01-12-2019'),
  209. country_name == 'Thailand' ~ dmy('13-01-2020'),
  210. country_name == 'Japan' ~ dmy('16-01-2020'),
  211. country_name == 'South Korea' ~ dmy('20-01-2020'),
  212. country_name == 'United States' ~ dmy('20-01-2020'),
  213. country_name == 'Taiwan' ~ dmy('21-01-2020'),
  214. country_name == 'Hong Kong' ~ dmy('22-01-2020'),
  215. country_name == 'Singapore' ~ dmy('23-01-2020'),
  216. country_name == 'Vietnam' ~ dmy('23-01-2020'),
  217. country_name == 'France' ~ dmy('24-01-2020'),
  218. country_name == 'Nepal' ~ dmy('24-01-2020'),
  219. country_name == 'Australia' ~ dmy('25-01-2020'),
  220. country_name == 'Canada' ~ dmy('25-01-2020'),
  221. country_name == 'Malaysia' ~ dmy('25-01-2020'),
  222. country_name == 'Cambodia' ~ dmy('27-01-2020'),
  223. country_name == 'Germany' ~ dmy('27-01-2020'),
  224. country_name == 'Sri Lanka' ~ dmy('27-01-2020'),
  225. country_name == 'Finalnd' ~ dmy('29-01-2020'),
  226. country_name == 'United Arab Emirates' ~ dmy('29-01-2020'),
  227. country_name == 'India' ~ dmy('30-01-2020'),
  228. country_name == 'Italy' ~ dmy('30-01-2020'),
  229. country_name == 'Philippines' ~ dmy('30-01-2020'),
  230. country_name == 'Spain' ~ dmy('31-01-2020'),
  231. country_name == 'Sweden' ~ dmy('31-01-2020'),
  232. country_name == 'United Kingdom' ~ dmy('31-01-2020'),
  233. country_name == 'Belgium' ~ dmy('04-02-2020'),
  234. country_name == 'Egypt' ~ dmy('14-01-2020'),
  235. TRUE ~ first_case_date
  236. )
  237. ) -> preprocessed_dataset
  238. # Fix n_days since 1st case for countries that had 1st case before Feb 11
  239. countries <- c('Thailand', 'Japan', 'South Korea', 'United States', 'Taiwan',
  240. 'Hong Kong', 'Singapore', 'Vietnam', 'France', 'Nepal',
  241. 'Australia', 'Canada', 'Malaysia', 'Cambodia', 'Germany',
  242. 'Sri Lanka', 'Finland', 'United Arab Emirates', 'India',
  243. 'Italy', 'Philippines', 'Spain', 'Sweden', 'United Kingdom',
  244. 'Belgium', 'Egypt')
  245. preprocessed_dataset %>%
  246. group_by(country_name) %>%
  247. mutate(n_days_since_1st_case =
  248. if_else(country_name %in% countries,
  249. as.numeric(date - first_case_date)+1,
  250. n_days_since_1st_case)) %>%
  251. ungroup() -> preprocessed_dataset
  252. rm(countries)
  253. # Saving final preprocessed dataset ---------------------------------------
  254. # Save full dataset
  255. write_tsv(x = preprocessed_dataset, path = 'data/preprocessed/DIB_dataset.tsv', quote_escape = FALSE)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...