bars-summary.R 13 KB

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