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 14 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  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('locality_code', 'locality_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', 'c'),
  21. delim = ',')
  22. colnames(covid) <- c('date', 'day', 'month', 'year', 'new_cases', 'new_deaths',
  23. 'locality_name', 'country_id', 'territory_id',
  24. 'pop_data_2018', 'continent')
  25. # COVID-19 data for Réunion and Hong Kong that are missing in the ECDC dataset
  26. # Source: John Hopkins University https://github.com/CSSEGISandData/COVID-19
  27. covid_hk_re <- read_delim(file = 'data/raw/hk-reunion-covid-19.csv',
  28. col_types = cols('c', 'c', 'd', 'd'),
  29. delim = ',')
  30. # Country details from UN Data
  31. country_details <- read_delim(file = 'data/raw/UN_dataset.tsv', delim = '\t',
  32. col_types = paste(c('c',
  33. rep('d', 173),
  34. 'c'),
  35. collapse=''))
  36. # Preprocessing step ---------------------------------------------------------
  37. ####
  38. #
  39. # Working on GMR dataset
  40. #
  41. ####
  42. # Create a long table from the wide original version
  43. preprocessed_dataset <- pivot_longer(raw_dataset, cols=6:11, names_to = 'plot_name',
  44. values_to = 'variation')
  45. rm(raw_dataset)
  46. ####
  47. #
  48. # Working on COVID19 dataset
  49. #
  50. ####
  51. covid %>%
  52. # These columns are useless
  53. select(-c('day', 'month', 'year', 'country_id', 'territory_id',
  54. 'pop_data_2018', 'continent')) %>%
  55. # Convert date to Date
  56. mutate(date, date = dmy(covid$date)) %>%
  57. # Replace _ by space in country names
  58. mutate(locality_name,
  59. locality_name = gsub('_',
  60. ' ',
  61. locality_name)) -> covid
  62. covid_hk_re %>%
  63. # Convert date to Date
  64. mutate(date, date = mdy(covid_hk_re$date)) -> covid_hk_re
  65. covid <- rbind(covid, covid_hk_re)
  66. rm(covid_hk_re)
  67. # Some countries have different names between the datasets
  68. covid %>%
  69. mutate(locality_name, locality_name = case_when(
  70. locality_name == 'United States of America' ~ 'United States',
  71. locality_name == 'United Republic of Tanzania' ~ 'Tanzania',
  72. locality_name == 'Guinea Bissau' ~ 'Guinea-Bissau',
  73. locality_name == 'Bahamas' ~ 'The Bahamas',
  74. locality_name == 'Myanmar' ~ 'Myanmar (Burma)',
  75. locality_name == 'Cote dIvoire' ~ 'Côte d\'Ivoire',
  76. TRUE ~ locality_name)
  77. ) -> covid
  78. # Some rows are missing like March 2nd and 3rd for Brazil. Apparently,
  79. # this happens when there is no case/death. Complete with these rows.
  80. covid %>%
  81. complete(date = seq.Date(min(date), max(date), by='day'), locality_name) %>%
  82. mutate(new_cases = ifelse(is.na(new_cases), 0, new_cases)) %>%
  83. mutate(new_deaths = ifelse(is.na(new_deaths), 0, new_deaths)) -> covid
  84. ####
  85. #
  86. # Merge World COVID19 and GMR datasets
  87. #
  88. ####
  89. preprocessed_dataset <- left_join(preprocessed_dataset, covid,
  90. by = c('date', 'locality_name'))
  91. rm(covid)
  92. ####
  93. #
  94. # Working on dataset after 1st merge
  95. #
  96. ####
  97. # GMR has data on Tajikistan. Updates info
  98. # Tajikistan has not had any case related to COVID-19 so far.
  99. # https://reliefweb.int/report/tajikistan/covid-19-tajikistan-situation-report-4-27-april-2020
  100. preprocessed_dataset %>%
  101. mutate(new_cases = ifelse(locality_name == 'Tajikistan',
  102. 0, new_cases)) %>%
  103. mutate(new_deaths = ifelse(locality_name == 'Tajikistan',
  104. 0, new_deaths)) -> preprocessed_dataset
  105. # Create column with accumulate cases/death
  106. preprocessed_dataset %>%
  107. group_by(locality_name, plot_name, region_name) %>%
  108. arrange(date) %>%
  109. dplyr::mutate(acc_cases = cumsum(new_cases)) %>%
  110. dplyr::mutate(acc_deaths = cumsum(new_deaths)) %>%
  111. ungroup -> preprocessed_dataset
  112. # Set new_cases and new_deaths to NA for regions, because our COVID19 dataset
  113. # only has data for countries.
  114. preprocessed_dataset %>%
  115. mutate(new_cases = ifelse(!is.na(region_name),
  116. NA,
  117. new_cases)) %>%
  118. mutate(new_deaths = ifelse(!is.na(region_name),
  119. NA,
  120. new_deaths)) -> preprocessed_dataset
  121. ####
  122. #
  123. # Working on country details dataset (UN)
  124. #
  125. ####
  126. # Before merging to get more info about the countries, we must make sure all
  127. # country names are the same.
  128. #unique(preprocessed_dataset$locality_name)[which(
  129. # unique(preprocessed_dataset$locality_name) %in%
  130. # unique(country_details$region_name) == FALSE
  131. # )
  132. # ]
  133. country_details %>%
  134. mutate(region_name = case_when(
  135. region_name == 'Bolivia (Plurinational State of)' ~ 'Bolivia',
  136. region_name == 'Bahamas' ~ 'The Bahamas',
  137. region_name == 'Republic of Korea' ~ 'South Korea',
  138. region_name == 'Cabo Verde' ~ 'Cape Verde',
  139. region_name == 'China, Hong Kong SAR' ~ 'Hong Kong',
  140. region_name == 'Republic of Moldova' ~ 'Moldova',
  141. region_name == 'Lao People\'s Democratic Republic' ~ 'Laos',
  142. region_name == 'Myanmar' ~ 'Myanmar (Burma)',
  143. region_name == 'United Rep. of Tanzania' ~ 'Tanzania',
  144. region_name == 'United States of America' ~ 'United States',
  145. region_name == 'Venezuela (Boliv. Rep. of)' ~ 'Venezuela',
  146. region_name == 'Viet Nam' ~ 'Vietnam',
  147. TRUE ~ region_name)
  148. ) -> country_details
  149. # In some datastes US appears as United States, and in others as United States
  150. # of America. The naming was fixed earlier, but we have two rows for US. Fix.
  151. # ids <- which(country_details$region_name == 'United States')
  152. # Some variables are there for US, others are there only for United States.
  153. # Merge.
  154. country_details[88,][,17:42] <- country_details[217,][,17:42]
  155. country_details[88,][,45:51] <- country_details[217,][,45:51]
  156. country_details[88,][,53:55] <- country_details[217,][,53:55]
  157. country_details[88,][,63:72] <- country_details[217,][,63:72]
  158. country_details[88,][,77:80] <- country_details[217,][,77:80]
  159. country_details[88,][,90:93] <- country_details[217,][,90:93]
  160. country_details[88,][,97:104] <- country_details[217,][,97:104]
  161. country_details[88,][,112:114] <- country_details[217,][,112:114]
  162. country_details[88,][,116:118] <- country_details[217,][,116:118]
  163. country_details[88,][,128:133] <- country_details[217,][,128:133]
  164. country_details[88,][,143:148] <- country_details[217,][,143:148]
  165. country_details[88,][,150] <- country_details[217,][,150]
  166. country_details[88,][,152:157] <- country_details[217,][,152:157]
  167. country_details[88,][,159:175] <- country_details[217,][,159:175]
  168. country_details <- country_details[-217, ]
  169. ####
  170. #
  171. # Merge country details and preprocessed_dataset
  172. #
  173. ####
  174. preprocessed_dataset <- left_join(preprocessed_dataset, country_details,
  175. by = c('locality_name' = 'region_name'))
  176. rm(country_details)
  177. # Remove rows that are related to regions
  178. preprocessed_dataset %>%
  179. filter(is.na(region_name)) %>%
  180. select(-c('region_name', 'county_name')) -> preprocessed_dataset
  181. colnames(preprocessed_dataset)[10:182] %>%
  182. # Make them all lowercase
  183. tolower %>%
  184. # Replace space by underscore
  185. gsub(' ', '_', .) %>%
  186. gsub('-', '_', .) %>%
  187. gsub('_+', '_', .) %>%
  188. gsub(',', '', .) -> colnames(preprocessed_dataset)[10:182]
  189. # Create columns for lethality rate
  190. preprocessed_dataset %>%
  191. group_by(locality_name) %>%
  192. arrange(date) %>%
  193. mutate(lethality_rate_percent = (acc_deaths[n()]/acc_cases[n()])*100) %>%
  194. ungroup() -> preprocessed_dataset
  195. # Bring plot_names from row to column
  196. preprocessed_dataset %>%
  197. pivot_wider(names_from = plot_name, values_from = variation) -> preprocessed_dataset
  198. # Add n_days_since_1st_case column
  199. preprocessed_dataset %>%
  200. group_by(locality_name) %>%
  201. mutate(first_case_date = min(date[acc_cases > 0])) %>%
  202. mutate(n_days_since_1st_case =
  203. if_else(acc_cases > 0,
  204. as.numeric(date - min(date[acc_cases > 0])),
  205. 0)) %>%
  206. ungroup() -> preprocessed_dataset
  207. # There is a warning here, because Tajikistan doesn't have any date with
  208. # cases. So the minimum of nothing will throw a warning.
  209. # preprocessed_dataset %>%
  210. # group_by(locality_name) %>%
  211. # arrange(date) %>%
  212. # filter(date == '2020-04-17') %>%
  213. # filter(acc_cases == 0) %>%
  214. # select(locality_name) %>%
  215. # unique
  216. # Add n_days_since_1st_death column
  217. preprocessed_dataset %>%
  218. group_by(locality_name) %>%
  219. mutate(first_death_date = min(date[acc_deaths > 0])) %>%
  220. mutate(n_days_since_1st_death =
  221. if_else(acc_deaths > 0,
  222. as.numeric(date - min(date[acc_deaths > 0])),
  223. 0)) %>%
  224. ungroup() -> preprocessed_dataset
  225. # There is a warning here, because 15 countries don't have any date with
  226. # deaths. So the minimum of nothing will throw a warning.
  227. # preprocessed_dataset %>%
  228. # group_by(locality_name) %>%
  229. # arrange(date) %>%
  230. # filter(date == '2020-04-17') %>%
  231. # filter(acc_deaths == 0) %>%
  232. # select(locality_name) %>%
  233. # unique
  234. # Set manually first case for countries whose first case happened before Feb 15
  235. # Wikipedia contributors, "2019–20 coronavirus pandemic", Wikipedia, The Free
  236. # Encyclopedia,
  237. # https://en.wikipedia.org/w/index.php?title=2019%E2%80%9320_coronavirus_pandemic&oldid=952187370
  238. # (accessed April 21, 2020).
  239. preprocessed_dataset %>%
  240. mutate(first_case_date = case_when(
  241. # locality_name == 'China' ~ dmy('01-12-2019'),
  242. locality_name == 'Thailand' ~ dmy('13-01-2020'),
  243. locality_name == 'Japan' ~ dmy('16-01-2020'),
  244. locality_name == 'South Korea' ~ dmy('20-01-2020'),
  245. locality_name == 'United States' ~ dmy('20-01-2020'),
  246. locality_name == 'Taiwan' ~ dmy('21-01-2020'),
  247. locality_name == 'Hong Kong' ~ dmy('22-01-2020'),
  248. locality_name == 'Singapore' ~ dmy('23-01-2020'),
  249. locality_name == 'Vietnam' ~ dmy('23-01-2020'),
  250. locality_name == 'France' ~ dmy('24-01-2020'),
  251. locality_name == 'Nepal' ~ dmy('24-01-2020'),
  252. locality_name == 'Australia' ~ dmy('25-01-2020'),
  253. locality_name == 'Canada' ~ dmy('25-01-2020'),
  254. locality_name == 'Malaysia' ~ dmy('25-01-2020'),
  255. locality_name == 'Cambodia' ~ dmy('27-01-2020'),
  256. locality_name == 'Germany' ~ dmy('27-01-2020'),
  257. locality_name == 'Sri Lanka' ~ dmy('27-01-2020'),
  258. locality_name == 'Finalnd' ~ dmy('29-01-2020'),
  259. locality_name == 'United Arab Emirates' ~ dmy('29-01-2020'),
  260. locality_name == 'India' ~ dmy('30-01-2020'),
  261. locality_name == 'Italy' ~ dmy('30-01-2020'),
  262. locality_name == 'Philippines' ~ dmy('30-01-2020'),
  263. locality_name == 'Spain' ~ dmy('31-01-2020'),
  264. locality_name == 'Sweden' ~ dmy('31-01-2020'),
  265. locality_name == 'United Kingdom' ~ dmy('31-01-2020'),
  266. locality_name == 'Belgium' ~ dmy('04-02-2020'),
  267. locality_name == 'Egypt' ~ dmy('14-01-2020'),
  268. TRUE ~ first_case_date
  269. )
  270. ) -> preprocessed_dataset
  271. # Fix n_days since 1st case for countries that had 1st case before Feb 11
  272. countries <- c('Thailand', 'Japan', 'South Korea', 'United States', 'Taiwan',
  273. 'Hong Kong', 'Singapore', 'Vietnam', 'France', 'Nepal',
  274. 'Australia', 'Canada', 'Malaysia', 'Cambodia', 'Germany',
  275. 'Sri Lanka', 'Finland', 'United Arab Emirates', 'India',
  276. 'Italy', 'Philippines', 'Spain', 'Sweden', 'United Kingdom',
  277. 'Belgium', 'Egypt')
  278. preprocessed_dataset %>%
  279. group_by(locality_name) %>%
  280. mutate(n_days_since_1st_case =
  281. if_else(locality_name %in% countries,
  282. as.numeric(date - first_case_date)+1,
  283. n_days_since_1st_case)) %>%
  284. ungroup() -> preprocessed_dataset
  285. rm(countries)
  286. # Check documentation and see if variable names are correct ###
  287. # According to the file below, maternal mortality rate is per 100k livebirths,
  288. # not 100k people.
  289. # SYB62_246_201907_Population growth and indicators of fertility and mortality.pdf
  290. id = which(colnames(preprocessed_dataset) ==
  291. 'maternal_mortality_ratio_(deaths_per_100000_population)_2015')
  292. colnames(preprocessed_dataset)[id] <- paste0('maternal_mortality_ratio_(deaths',
  293. '_per_100000_livebirths)_2015')
  294. # According to the file below, it's participation rate.
  295. # SYB62_329_201904_Labour Force and Unemployment.pdf
  296. id = which(colnames(preprocessed_dataset) == paste0('labour_force_participatio',
  297. 'n_total_2019'))
  298. colnames(preprocessed_dataset)[id] <- paste0('labour_force_participation_rate_',
  299. 'total_2019')
  300. id = which(colnames(preprocessed_dataset) == paste0('labour_force_participatio',
  301. 'n_female_2019'))
  302. colnames(preprocessed_dataset)[id] <- paste0('labour_force_participation_rate_',
  303. 'female_2019')
  304. id = which(colnames(preprocessed_dataset) == paste0('labour_force_participatio',
  305. 'n_male_2019'))
  306. colnames(preprocessed_dataset)[id] <- paste0('labour_force_participation_rate_',
  307. 'male_2019')
  308. # Saving final preprocessed dataset ---------------------------------------
  309. # Save full dataset
  310. 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...