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