bars-summary.R 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #!/usr/bin/env Rscript
  2. library(tidyverse)
  3. library(lubridate)
  4. library(padr)
  5. complete <- read_csv2("../app/data/completo.csv", na = "NA",
  6. col_types = cols(
  7. pais = col_character(),
  8. regiao = col_character(),
  9. uf = col_character(),
  10. mesorregiao = col_character(),
  11. microrregiao = col_character(),
  12. municipio = col_character(),
  13. st_acidente_feriado = col_character(),
  14. ds_agente_causador = col_character(),
  15. ano_cat = col_integer(),
  16. ds_cnae_classe_cat = col_character(),
  17. dt_acidente = col_date(),
  18. st_dia_semana_acidente = col_character(),
  19. ds_emitente_cat = col_character(),
  20. hora_acidente = col_time(),
  21. idade_cat = col_integer(),
  22. cd_indica_obito = col_character(),
  23. ds_natureza_lesao = col_character(),
  24. ds_cbo = col_character(),
  25. ds_parte_corpo_atingida = col_character(),
  26. cd_tipo_sexo_empregado_cat = col_character(),
  27. ds_tipo_acidente = col_character(),
  28. ds_tipo_local_acidente = col_character()
  29. ))
  30. #Summarization of the number of accidents occurred by day, each year
  31. #2012
  32. ac_mun_2012 <- complete %>%
  33. group_by(microrregiao, municipio, ano_cat) %>%
  34. filter(ano_cat == 2012) %>%
  35. count(dt_acidente) %>%
  36. pad(start_val = ymd("2012-01-01"),
  37. end_val = ymd("2012-12-31"),
  38. break_above = 2) %>%
  39. fill_by_value(n, 0) %>% ungroup() %>%
  40. select(microrregiao, municipio, n)
  41. ac_micro_2012 <- complete %>%
  42. group_by(mesorregiao, microrregiao, ano_cat) %>%
  43. filter(ano_cat == 2012) %>%
  44. count(dt_acidente) %>%
  45. pad(start_val = ymd("2012-01-01"),
  46. end_val = ymd("2012-12-31"),
  47. break_above = 2) %>%
  48. fill_by_value(n, 0) %>% ungroup() %>%
  49. select(mesorregiao, microrregiao, n)
  50. ac_meso_2012 <- complete %>%
  51. group_by(uf, mesorregiao, ano_cat) %>%
  52. filter(ano_cat == 2012) %>%
  53. count(dt_acidente) %>%
  54. pad(start_val = ymd("2012-01-01"),
  55. end_val = ymd("2012-12-31"),
  56. break_above = 2) %>%
  57. fill_by_value(n, 0) %>% ungroup() %>%
  58. select(uf, mesorregiao, n)
  59. ac_uf_2012 <- complete %>%
  60. group_by(regiao, uf, ano_cat) %>%
  61. filter(ano_cat == 2012) %>%
  62. count(dt_acidente) %>%
  63. pad(start_val = ymd("2012-01-01"),
  64. end_val = ymd("2012-12-31"),
  65. break_above = 2) %>%
  66. fill_by_value(n, 0) %>% ungroup() %>%
  67. select(regiao, uf, n)
  68. ac_regiao_2012 <- complete %>%
  69. group_by(pais, regiao, ano_cat) %>%
  70. filter(ano_cat == 2012) %>%
  71. count(dt_acidente) %>%
  72. pad(start_val = ymd("2012-01-01"),
  73. end_val = ymd("2012-12-31"),
  74. break_above = 2) %>%
  75. fill_by_value(n, 0) %>% ungroup() %>%
  76. select(pais, regiao, n)
  77. ac_pais_2012 <- complete %>%
  78. group_by(pais, ano_cat) %>%
  79. filter(ano_cat == 2012) %>%
  80. count(dt_acidente) %>%
  81. pad(start_val = ymd("2012-01-01"),
  82. end_val = ymd("2012-12-31"),
  83. break_above = 2) %>%
  84. fill_by_value(n, 0) %>% ungroup() %>%
  85. select(pais, n)
  86. #2013
  87. ac_mun_2013 <- complete %>%
  88. group_by(microrregiao, municipio, ano_cat) %>%
  89. filter(ano_cat == 2013) %>%
  90. count(dt_acidente) %>%
  91. pad(start_val = ymd("2013-01-01"),
  92. end_val = ymd("2013-12-31"),
  93. break_above = 2) %>%
  94. fill_by_value(n, 0) %>% ungroup() %>%
  95. select(microrregiao, municipio, n)
  96. ac_micro_2013 <- complete %>%
  97. group_by(mesorregiao, microrregiao, ano_cat) %>%
  98. filter(ano_cat == 2013) %>%
  99. count(dt_acidente) %>%
  100. pad(start_val = ymd("2013-01-01"),
  101. end_val = ymd("2013-12-31"),
  102. break_above = 2) %>%
  103. fill_by_value(n, 0) %>% ungroup() %>%
  104. select(mesorregiao, microrregiao, n)
  105. ac_meso_2013 <- complete %>%
  106. group_by(uf, mesorregiao, ano_cat) %>%
  107. filter(ano_cat == 2013) %>%
  108. count(dt_acidente) %>%
  109. pad(start_val = ymd("2013-01-01"),
  110. end_val = ymd("2013-12-31"),
  111. break_above = 2) %>%
  112. fill_by_value(n, 0) %>% ungroup() %>%
  113. select(uf, mesorregiao, n)
  114. ac_uf_2013 <- complete %>%
  115. group_by(regiao, uf, ano_cat) %>%
  116. filter(ano_cat == 2013) %>%
  117. count(dt_acidente) %>%
  118. pad(start_val = ymd("2013-01-01"),
  119. end_val = ymd("2013-12-31"),
  120. break_above = 2) %>%
  121. fill_by_value(n, 0) %>% ungroup() %>%
  122. select(regiao, uf, n)
  123. ac_regiao_2013 <- complete %>%
  124. group_by(pais, regiao, ano_cat) %>%
  125. filter(ano_cat == 2013) %>%
  126. count(dt_acidente) %>%
  127. pad(start_val = ymd("2013-01-01"),
  128. end_val = ymd("2013-12-31"),
  129. break_above = 2) %>%
  130. fill_by_value(n, 0) %>% ungroup() %>%
  131. select(pais, regiao, n)
  132. ac_pais_2013 <- complete %>%
  133. group_by(pais, ano_cat) %>%
  134. filter(ano_cat == 2013) %>%
  135. count(dt_acidente) %>%
  136. pad(start_val = ymd("2013-01-01"),
  137. end_val = ymd("2013-12-31"),
  138. break_above = 2) %>%
  139. fill_by_value(n, 0) %>% ungroup() %>%
  140. select(pais, n)
  141. #2014
  142. ac_mun_2014 <- complete %>%
  143. group_by(microrregiao, municipio, ano_cat) %>%
  144. filter(ano_cat == 2014) %>%
  145. count(dt_acidente) %>%
  146. pad(start_val = ymd("2014-01-01"),
  147. end_val = ymd("2014-12-31"),
  148. break_above = 2) %>%
  149. fill_by_value(n, 0) %>% ungroup() %>%
  150. select(microrregiao, municipio, n)
  151. ac_micro_2014 <- complete %>%
  152. group_by(mesorregiao, microrregiao, ano_cat) %>%
  153. filter(ano_cat == 2014) %>%
  154. count(dt_acidente) %>%
  155. pad(start_val = ymd("2014-01-01"),
  156. end_val = ymd("2014-12-31"),
  157. break_above = 2) %>%
  158. fill_by_value(n, 0) %>% ungroup() %>%
  159. select(mesorregiao, microrregiao, n)
  160. ac_meso_2014 <- complete %>%
  161. group_by(uf, mesorregiao, ano_cat) %>%
  162. filter(ano_cat == 2014) %>%
  163. count(dt_acidente) %>%
  164. pad(start_val = ymd("2014-01-01"),
  165. end_val = ymd("2014-12-31"),
  166. break_above = 2) %>%
  167. fill_by_value(n, 0) %>% ungroup() %>%
  168. select(uf, mesorregiao, n)
  169. ac_uf_2014 <- complete %>%
  170. group_by(regiao, uf, ano_cat) %>%
  171. filter(ano_cat == 2014) %>%
  172. count(dt_acidente) %>%
  173. pad(start_val = ymd("2014-01-01"),
  174. end_val = ymd("2014-12-31"),
  175. break_above = 2) %>%
  176. fill_by_value(n, 0) %>% ungroup() %>%
  177. select(regiao, uf, n)
  178. ac_regiao_2014 <- complete %>%
  179. group_by(pais, regiao, ano_cat) %>%
  180. filter(ano_cat == 2014) %>%
  181. count(dt_acidente) %>%
  182. pad(start_val = ymd("2014-01-01"),
  183. end_val = ymd("2014-12-31"),
  184. break_above = 2) %>%
  185. fill_by_value(n, 0) %>% ungroup() %>%
  186. select(pais, regiao, n)
  187. ac_pais_2014 <- complete %>%
  188. group_by(pais, ano_cat) %>%
  189. filter(ano_cat == 2014) %>%
  190. count(dt_acidente) %>%
  191. pad(start_val = ymd("2014-01-01"),
  192. end_val = ymd("2014-12-31"),
  193. break_above = 2) %>%
  194. fill_by_value(n, 0) %>% ungroup() %>%
  195. select(pais, n)
  196. #2015
  197. ac_mun_2015 <- complete %>%
  198. group_by(microrregiao, municipio, ano_cat) %>%
  199. filter(ano_cat == 2015) %>%
  200. count(dt_acidente) %>%
  201. pad(start_val = ymd("2015-01-01"),
  202. end_val = ymd("2015-12-31"),
  203. break_above = 2) %>%
  204. fill_by_value(n, 0) %>% ungroup() %>%
  205. select(microrregiao, municipio, n)
  206. ac_micro_2015 <- complete %>%
  207. group_by(mesorregiao, microrregiao, ano_cat) %>%
  208. filter(ano_cat == 2015) %>%
  209. count(dt_acidente) %>%
  210. pad(start_val = ymd("2015-01-01"),
  211. end_val = ymd("2015-12-31"),
  212. break_above = 2) %>%
  213. fill_by_value(n, 0) %>% ungroup() %>%
  214. select(mesorregiao, microrregiao, n)
  215. ac_meso_2015 <- complete %>%
  216. group_by(uf, mesorregiao, ano_cat) %>%
  217. filter(ano_cat == 2015) %>%
  218. count(dt_acidente) %>%
  219. pad(start_val = ymd("2015-01-01"),
  220. end_val = ymd("2015-12-31"),
  221. break_above = 2) %>%
  222. fill_by_value(n, 0) %>% ungroup() %>%
  223. select(uf, mesorregiao, n)
  224. ac_uf_2015 <- complete %>%
  225. group_by(regiao, uf, ano_cat) %>%
  226. filter(ano_cat == 2015) %>%
  227. count(dt_acidente) %>%
  228. pad(start_val = ymd("2015-01-01"),
  229. end_val = ymd("2015-12-31"),
  230. break_above = 2) %>%
  231. fill_by_value(n, 0) %>% ungroup() %>%
  232. select(regiao, uf, n)
  233. ac_regiao_2015 <- complete %>%
  234. group_by(pais, regiao, ano_cat) %>%
  235. filter(ano_cat == 2015) %>%
  236. count(dt_acidente) %>%
  237. pad(start_val = ymd("2015-01-01"),
  238. end_val = ymd("2015-12-31"),
  239. break_above = 2) %>%
  240. fill_by_value(n, 0) %>% ungroup() %>%
  241. select(pais, regiao, n)
  242. ac_pais_2015 <- complete %>%
  243. group_by(pais, ano_cat) %>%
  244. filter(ano_cat == 2015) %>%
  245. count(dt_acidente) %>%
  246. pad(start_val = ymd("2015-01-01"),
  247. end_val = ymd("2015-12-31"),
  248. break_above = 2) %>%
  249. fill_by_value(n, 0) %>% ungroup() %>%
  250. select(pais, n)
  251. #2016
  252. ac_mun_2016 <- complete %>%
  253. group_by(microrregiao, municipio, ano_cat) %>%
  254. filter(ano_cat == 2016) %>%
  255. count(dt_acidente) %>%
  256. pad(start_val = ymd("2016-01-01"),
  257. end_val = ymd("2016-12-31"),
  258. break_above = 2) %>%
  259. fill_by_value(n, 0) %>% ungroup() %>%
  260. select(microrregiao, municipio, n)
  261. ac_micro_2016 <- complete %>%
  262. group_by(mesorregiao, microrregiao, ano_cat) %>%
  263. filter(ano_cat == 2016) %>%
  264. count(dt_acidente) %>%
  265. pad(start_val = ymd("2016-01-01"),
  266. end_val = ymd("2016-12-31"),
  267. break_above = 2) %>%
  268. fill_by_value(n, 0) %>% ungroup() %>%
  269. select(mesorregiao, microrregiao, n)
  270. ac_meso_2016 <- complete %>%
  271. group_by(uf, mesorregiao, ano_cat) %>%
  272. filter(ano_cat == 2016) %>%
  273. count(dt_acidente) %>%
  274. pad(start_val = ymd("2016-01-01"),
  275. end_val = ymd("2016-12-31"),
  276. break_above = 2) %>%
  277. fill_by_value(n, 0) %>% ungroup() %>%
  278. select(uf, mesorregiao, n)
  279. ac_uf_2016 <- complete %>%
  280. group_by(regiao, uf, ano_cat) %>%
  281. filter(ano_cat == 2016) %>%
  282. count(dt_acidente) %>%
  283. pad(start_val = ymd("2016-01-01"),
  284. end_val = ymd("2016-12-31"),
  285. break_above = 2) %>%
  286. fill_by_value(n, 0) %>% ungroup() %>%
  287. select(regiao, uf, n)
  288. ac_regiao_2016 <- complete %>%
  289. group_by(pais, regiao, ano_cat) %>%
  290. filter(ano_cat == 2016) %>%
  291. count(dt_acidente) %>%
  292. pad(start_val = ymd("2016-01-01"),
  293. end_val = ymd("2016-12-31"),
  294. break_above = 2) %>%
  295. fill_by_value(n, 0) %>% ungroup() %>%
  296. select(pais, regiao, n)
  297. ac_pais_2016 <- complete %>%
  298. group_by(pais, ano_cat) %>%
  299. filter(ano_cat == 2016) %>%
  300. count(dt_acidente) %>%
  301. pad(start_val = ymd("2016-01-01"),
  302. end_val = ymd("2016-12-31"),
  303. break_above = 2) %>%
  304. fill_by_value(n, 0) %>% ungroup() %>%
  305. select(pais, n)
  306. #2017
  307. ac_mun_2017 <- complete %>%
  308. group_by(microrregiao, municipio, ano_cat) %>%
  309. filter(ano_cat == 2017) %>%
  310. count(dt_acidente) %>%
  311. pad(start_val = ymd("2017-01-01"),
  312. end_val = ymd("2017-12-31"),
  313. break_above = 2) %>%
  314. fill_by_value(n, 0) %>% ungroup() %>%
  315. select(microrregiao, municipio, n)
  316. ac_micro_2017 <- complete %>%
  317. group_by(mesorregiao, microrregiao, ano_cat) %>%
  318. filter(ano_cat == 2017) %>%
  319. count(dt_acidente) %>%
  320. pad(start_val = ymd("2017-01-01"),
  321. end_val = ymd("2017-12-31"),
  322. break_above = 2) %>%
  323. fill_by_value(n, 0) %>% ungroup() %>%
  324. select(mesorregiao, microrregiao, n)
  325. ac_meso_2017 <- complete %>%
  326. group_by(uf, mesorregiao, ano_cat) %>%
  327. filter(ano_cat == 2017) %>%
  328. count(dt_acidente) %>%
  329. pad(start_val = ymd("2017-01-01"),
  330. end_val = ymd("2017-12-31"),
  331. break_above = 2) %>%
  332. fill_by_value(n, 0) %>% ungroup() %>%
  333. select(uf, mesorregiao, n)
  334. ac_uf_2017 <- complete %>%
  335. group_by(regiao, uf, ano_cat) %>%
  336. filter(ano_cat == 2017) %>%
  337. count(dt_acidente) %>%
  338. pad(start_val = ymd("2017-01-01"),
  339. end_val = ymd("2017-12-31"),
  340. break_above = 2) %>%
  341. fill_by_value(n, 0) %>% ungroup() %>%
  342. select(regiao, uf, n)
  343. ac_regiao_2017 <- complete %>%
  344. group_by(pais, regiao, ano_cat) %>%
  345. filter(ano_cat == 2017) %>%
  346. count(dt_acidente) %>%
  347. pad(start_val = ymd("2017-01-01"),
  348. end_val = ymd("2017-12-31"),
  349. break_above = 2) %>%
  350. fill_by_value(n, 0) %>% ungroup() %>%
  351. select(pais, regiao, n)
  352. ac_pais_2017 <- complete %>%
  353. group_by(pais, ano_cat) %>%
  354. filter(ano_cat == 2017) %>%
  355. count(dt_acidente) %>%
  356. pad(start_val = ymd("2017-01-01"),
  357. end_val = ymd("2017-12-31"),
  358. break_above = 2) %>%
  359. fill_by_value(n, 0) %>% ungroup() %>%
  360. select(pais, n)
  361. # Write all csvs
  362. # 2012
  363. write_delim(ac_mun_2012, "../app/data/barchart/ac_mun_2012.csv", delim = ";")
  364. write_delim(ac_micro_2012, "../app/data/barchart/ac_micro_2012.csv", delim = ";")
  365. write_delim(ac_meso_2012, "../app/data/barchart/ac_meso_2012.csv", delim = ";")
  366. write_delim(ac_uf_2012, "../app/data/barchart/ac_uf_2012.csv", delim = ";")
  367. write_delim(ac_regiao_2012, "../app/data/barchart/ac_regiao_2012.csv", delim = ";")
  368. write_delim(ac_pais_2012, "../app/data/barchart/ac_pais_2012.csv", delim = ";")
  369. # 2013
  370. write_delim(ac_mun_2013, "../app/data/barchart/ac_mun_2013.csv", delim = ";")
  371. write_delim(ac_micro_2013, "../app/data/barchart/ac_micro_2013.csv", delim = ";")
  372. write_delim(ac_meso_2013, "../app/data/barchart/ac_meso_2013.csv", delim = ";")
  373. write_delim(ac_uf_2013, "../app/data/barchart/ac_uf_2013.csv", delim = ";")
  374. write_delim(ac_regiao_2013, "../app/data/barchart/ac_regiao_2013.csv", delim = ";")
  375. write_delim(ac_pais_2013, "../app/data/barchart/ac_pais_2013.csv", delim = ";")
  376. # 2014
  377. write_delim(ac_mun_2014, "../app/data/barchart/ac_mun_2014.csv", delim = ";")
  378. write_delim(ac_micro_2014, "../app/data/barchart/ac_micro_2014.csv", delim = ";")
  379. write_delim(ac_meso_2014, "../app/data/barchart/ac_meso_2014.csv", delim = ";")
  380. write_delim(ac_uf_2014, "../app/data/barchart/ac_uf_2014.csv", delim = ";")
  381. write_delim(ac_regiao_2014, "../app/data/barchart/ac_regiao_2014.csv", delim = ";")
  382. write_delim(ac_pais_2014, "../app/data/barchart/ac_pais_2014.csv", delim = ";")
  383. # 2015
  384. write_delim(ac_mun_2015, "../app/data/barchart/ac_mun_2015.csv", delim = ";")
  385. write_delim(ac_micro_2015, "../app/data/barchart/ac_micro_2015.csv", delim = ";")
  386. write_delim(ac_meso_2015, "../app/data/barchart/ac_meso_2015.csv", delim = ";")
  387. write_delim(ac_uf_2015, "../app/data/barchart/ac_uf_2015.csv", delim = ";")
  388. write_delim(ac_regiao_2015, "../app/data/barchart/ac_regiao_2015.csv", delim = ";")
  389. write_delim(ac_pais_2015, "../app/data/barchart/ac_pais_2015.csv", delim = ";")
  390. # 2016
  391. write_delim(ac_mun_2016, "../app/data/barchart/ac_mun_2016.csv", delim = ";")
  392. write_delim(ac_micro_2016, "../app/data/barchart/ac_micro_2016.csv", delim = ";")
  393. write_delim(ac_meso_2016, "../app/data/barchart/ac_meso_2016.csv", delim = ";")
  394. write_delim(ac_uf_2016, "../app/data/barchart/ac_uf_2016.csv", delim = ";")
  395. write_delim(ac_regiao_2016, "../app/data/barchart/ac_regiao_2016.csv", delim = ";")
  396. write_delim(ac_pais_2016, "../app/data/barchart/ac_pais_2016.csv", delim = ";")
  397. # 2017
  398. write_delim(ac_mun_2017, "../app/data/barchart/ac_mun_2017.csv", delim = ";")
  399. write_delim(ac_micro_2017, "../app/data/barchart/ac_micro_2017.csv", delim = ";")
  400. write_delim(ac_meso_2017, "../app/data/barchart/ac_meso_2017.csv", delim = ";")
  401. write_delim(ac_uf_2017, "../app/data/barchart/ac_uf_2017.csv", delim = ";")
  402. write_delim(ac_regiao_2017, "../app/data/barchart/ac_regiao_2017.csv", delim = ";")
  403. write_delim(ac_pais_2017, "../app/data/barchart/ac_pais_2017.csv", delim = ";")