heatmap-table.r 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. c <- complete %>%
  31. filter(ano_cat == 2012) %>%
  32. select(uf, dt_acidente) %>%
  33. group_by(uf, dt_acidente) %>%
  34. summarize(acidentes = n()) %>%
  35. pad(start_val = ymd("2012-01-01"),
  36. end_val = ymd("2012-12-31"),
  37. break_above = 2) %>%
  38. fill_by_value(acidentes, 0) %>%
  39. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  40. select(uf, mes, dia, acidentes)
  41. c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
  42. write_delim(c, "../app/data/heatmap/br_mes_dia.csv_2012", delim = ";")
  43. # 2013
  44. c <- complete %>%
  45. filter(ano_cat == 2013) %>%
  46. select(uf, dt_acidente) %>%
  47. group_by(uf, dt_acidente) %>%
  48. summarize(acidentes = n()) %>%
  49. pad(start_val = ymd("2013-01-01"),
  50. end_val = ymd("2013-12-31"),
  51. break_above = 2) %>%
  52. fill_by_value(acidentes, 0) %>%
  53. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  54. select(uf, mes, dia, acidentes)
  55. c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
  56. write_delim(c, "../app/data/heatmap/br_mes_dia_2013.csv", delim = ";")
  57. #2014
  58. c <- complete %>%
  59. filter(ano_cat == 2014) %>%
  60. select(uf, dt_acidente) %>%
  61. group_by(uf, dt_acidente) %>%
  62. summarize(acidentes = n()) %>%
  63. pad(start_val = ymd("2014-01-01"),
  64. end_val = ymd("2014-12-31"),
  65. break_above = 2) %>%
  66. fill_by_value(acidentes, 0) %>%
  67. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  68. select(uf, mes, dia, acidentes)
  69. c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
  70. write_delim(c, "../app/data/heatmap/br_mes_dia_2014.csv", delim = ";")
  71. #2015
  72. c <- complete %>%
  73. filter(ano_cat == 2015) %>%
  74. select(uf, dt_acidente) %>%
  75. group_by(uf, dt_acidente) %>%
  76. summarize(acidentes = n()) %>%
  77. pad(start_val = ymd("2015-01-01"),
  78. end_val = ymd("2015-12-31"),
  79. break_above = 2) %>%
  80. fill_by_value(acidentes, 0) %>%
  81. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  82. select(uf, mes, dia, acidentes)
  83. c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
  84. write_delim(c, "../app/data/heatmap/br_mes_dia_2015.csv", delim = ";")
  85. #2016
  86. c <- complete %>%
  87. filter(ano_cat == 2016) %>%
  88. select(uf, dt_acidente) %>%
  89. group_by(uf, dt_acidente) %>%
  90. summarize(acidentes = n()) %>%
  91. pad(start_val = ymd("2016-01-01"),
  92. end_val = ymd("2016-12-31"),
  93. break_above = 2) %>%
  94. fill_by_value(acidentes, 0) %>%
  95. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  96. select(uf, mes, dia, acidentes)
  97. c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
  98. write_delim(c, "../app/data/heatmap/br_mes_dia_2016.csv", delim = ";")
  99. #2017
  100. c <- complete %>%
  101. filter(ano_cat == 2017) %>%
  102. select(uf, dt_acidente) %>%
  103. group_by(uf, dt_acidente) %>%
  104. summarize(acidentes = n()) %>%
  105. pad(start_val = ymd("2017-01-01"),
  106. end_val = ymd("2017-12-31"),
  107. break_above = 2) %>%
  108. fill_by_value(acidentes, 0) %>%
  109. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  110. select(uf, mes, dia, acidentes)
  111. c <- c[-1] %>% group_by(mes, dia) %>% summarize(acidentes = sum(acidentes))
  112. write_delim(c, "../app/data/heatmap/br_mes_dia_2017.csv", delim = ";")
  113. # Filter: By all states of Brazil and by year
  114. # 2012
  115. c <- complete %>%
  116. filter(ano_cat == 2012) %>%
  117. select(uf, dt_acidente) %>%
  118. group_by(uf, dt_acidente) %>%
  119. summarize(acidentes = n()) %>%
  120. pad(start_val = ymd("2012-01-01"),
  121. end_val = ymd("2012-12-31"),
  122. break_above = 2) %>%
  123. fill_by_value(acidentes, 0) %>%
  124. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  125. select(uf, mes, dia, acidentes) %>%
  126. group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
  127. write_delim(c, "../app/data/heatmap/estados_mes_dia_2012.csv", delim = ";")
  128. # 2013
  129. c <- complete %>%
  130. filter(ano_cat == 2013) %>%
  131. select(uf, dt_acidente) %>%
  132. group_by(uf, dt_acidente) %>%
  133. summarize(acidentes = n()) %>%
  134. pad(start_val = ymd("2013-01-01"),
  135. end_val = ymd("2013-12-31"),
  136. break_above = 2) %>%
  137. fill_by_value(acidentes, 0) %>%
  138. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  139. select(uf, mes, dia, acidentes) %>%
  140. group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
  141. write_delim(c, "../app/data/heatmap/estados_mes_dia_2013.csv", delim = ";")
  142. # 2014
  143. c <- complete %>%
  144. filter(ano_cat == 2014) %>%
  145. select(uf, dt_acidente) %>%
  146. group_by(uf, dt_acidente) %>%
  147. summarize(acidentes = n()) %>%
  148. pad(start_val = ymd("2014-01-01"),
  149. end_val = ymd("2014-12-31"),
  150. break_above = 2) %>%
  151. fill_by_value(acidentes, 0) %>%
  152. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  153. select(uf, mes, dia, acidentes) %>%
  154. group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
  155. write_delim(c, "../app/data/heatmap/estados_mes_dia_2014.csv", delim = ";")
  156. # 2015
  157. c <- complete %>%
  158. filter(ano_cat == 2015) %>%
  159. select(uf, dt_acidente) %>%
  160. group_by(uf, dt_acidente) %>%
  161. summarize(acidentes = n()) %>%
  162. pad(start_val = ymd("2015-01-01"),
  163. end_val = ymd("2015-12-31"),
  164. break_above = 2) %>%
  165. fill_by_value(acidentes, 0) %>%
  166. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  167. select(uf, mes, dia, acidentes) %>%
  168. group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
  169. write_delim(c, "../app/data/heatmap/estados_mes_dia_2015.csv", delim = ";")
  170. # 2016
  171. c <- complete %>%
  172. filter(ano_cat == 2016) %>%
  173. select(uf, dt_acidente) %>%
  174. group_by(uf, dt_acidente) %>%
  175. summarize(acidentes = n()) %>%
  176. pad(start_val = ymd("2016-01-01"),
  177. end_val = ymd("2016-12-31"),
  178. break_above = 2) %>%
  179. fill_by_value(acidentes, 0) %>%
  180. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  181. select(uf, mes, dia, acidentes) %>%
  182. group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
  183. write_delim(c, "../app/data/heatmap/estados_mes_dia_2016.csv", delim = ";")
  184. # 2017
  185. c <- complete %>%
  186. filter(ano_cat == 2017) %>%
  187. select(uf, dt_acidente) %>%
  188. group_by(uf, dt_acidente) %>%
  189. summarize(acidentes = n()) %>%
  190. pad(start_val = ymd("2017-01-01"),
  191. end_val = ymd("2017-12-31"),
  192. break_above = 2) %>%
  193. fill_by_value(acidentes, 0) %>%
  194. mutate(dia = day(dt_acidente), mes = month(dt_acidente)) %>%
  195. select(uf, mes, dia, acidentes) %>%
  196. group_by(uf, mes, dia) %>% summarize(acidentes = sum(acidentes))
  197. write_delim(c, "../app/data/heatmap/estados_mes_dia_2017.csv", delim = ";")