choropleth-mesos.R 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env Rscript
  2. library(tidyverse)
  3. library(lubridate)
  4. library(padr)
  5. print("Loading complete data file...")
  6. complete <- read_csv2("../app/data/completo.csv", na = "NA",
  7. col_types = cols(
  8. pais = col_character(),
  9. regiao = col_character(),
  10. uf = col_character(),
  11. mesorregiao = col_character(),
  12. microrregiao = col_character(),
  13. municipio = col_character(),
  14. st_acidente_feriado = col_character(),
  15. ds_agente_causador = col_character(),
  16. ano_cat = col_integer(),
  17. ds_cnae_classe_cat = col_character(),
  18. dt_acidente = col_date(),
  19. st_dia_semana_acidente = col_character(),
  20. ds_emitente_cat = col_character(),
  21. hora_acidente = col_time(),
  22. idade_cat = col_integer(),
  23. cd_indica_obito = col_character(),
  24. ds_natureza_lesao = col_character(),
  25. ds_cbo = col_character(),
  26. ds_parte_corpo_atingida = col_character(),
  27. cd_tipo_sexo_empregado_cat = col_character(),
  28. ds_tipo_acidente = col_character(),
  29. ds_tipo_local_acidente = col_character()
  30. ))
  31. #Estimation of population for each year by "municipio"
  32. print("Loading estimation files...")
  33. est2012 <- read_csv2("../app/data/estimativa_municipio_2012.csv",
  34. na = "NA",
  35. col_types = cols(
  36. uf = col_character(),
  37. municipio = col_character(),
  38. populacao = col_integer(),
  39. ano = col_integer()
  40. ))
  41. est2013 <- read_csv2("../app/data/estimativa_municipio_2013.csv",
  42. na = "NA",
  43. col_types = cols(
  44. uf = col_character(),
  45. municipio = col_character(),
  46. populacao = col_integer(),
  47. ano = col_integer()
  48. ))
  49. est2014 <- read_csv2("../app/data/estimativa_municipio_2014.csv",
  50. na = "NA",
  51. col_types = cols(
  52. uf = col_character(),
  53. municipio = col_character(),
  54. populacao = col_integer(),
  55. ano = col_integer()
  56. ))
  57. est2015 <- read_csv2("../app/data/estimativa_municipio_2015.csv",
  58. na = "NA",
  59. col_types = cols(
  60. uf = col_character(),
  61. municipio = col_character(),
  62. populacao = col_integer(),
  63. ano = col_integer()
  64. ))
  65. est2016 <- read_csv2("../app/data/estimativa_municipio_2016.csv",
  66. na = "NA",
  67. col_types = cols(
  68. uf = col_character(),
  69. municipio = col_character(),
  70. populacao = col_integer(),
  71. ano = col_integer()
  72. ))
  73. est2017 <- read_csv2("../app/data/estimativa_municipio_2017.csv",
  74. na = "NA",
  75. col_types = cols(
  76. uf = col_character(),
  77. municipio = col_character(),
  78. populacao = col_integer(),
  79. ano = col_integer()
  80. ))
  81. #Summarise population
  82. est12 <- est2012 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
  83. est13 <- est2013 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
  84. est14 <- est2014 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
  85. est15 <- est2015 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
  86. est16 <- est2016 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
  87. est17 <- est2017 %>% group_by(uf, municipio) %>% summarise(populacao = sum(populacao))
  88. #summarise accidents
  89. print("Summarising accidents...")
  90. acd12 <- complete %>%
  91. filter(ano_cat == 2012) %>%
  92. group_by(uf, mesorregiao, municipio) %>%
  93. summarise(acidentes = n())
  94. acd13 <- complete %>%
  95. filter(ano_cat == 2013) %>%
  96. group_by(uf, mesorregiao, municipio) %>%
  97. summarise(acidentes = n())
  98. acd14 <- complete %>%
  99. filter(ano_cat == 2014) %>%
  100. group_by(uf, mesorregiao, municipio) %>%
  101. summarise(acidentes = n())
  102. acd15 <- complete %>%
  103. filter(ano_cat == 2015) %>%
  104. group_by(uf, mesorregiao, municipio) %>%
  105. summarise(acidentes = n())
  106. acd16 <- complete %>%
  107. filter(ano_cat == 2016) %>%
  108. group_by(uf, mesorregiao, municipio) %>%
  109. summarise(acidentes = n())
  110. acd17 <- complete %>%
  111. filter(ano_cat == 2017) %>%
  112. group_by(uf, mesorregiao, municipio) %>%
  113. summarise(acidentes = n())
  114. #Join estimations tables and c
  115. print("Joining tables...")
  116. percent_est12 <- inner_join(acd12, est12, by = c("uf", "municipio")) %>%
  117. select(uf, mesorregiao, acidentes, populacao) %>%
  118. group_by(mesorregiao) %>%
  119. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  120. mutate(percentage = (acidentes/populacao) * 100)
  121. percent_est13 <- inner_join(acd13, est13, by = c("uf", "municipio")) %>%
  122. select(uf, mesorregiao, acidentes, populacao) %>%
  123. group_by(mesorregiao) %>%
  124. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  125. mutate(percentage = (acidentes/populacao) * 100)
  126. percent_est14 <- inner_join(acd14, est14, by = c("uf", "municipio")) %>%
  127. select(uf, mesorregiao, acidentes, populacao) %>%
  128. group_by(mesorregiao) %>%
  129. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  130. mutate(percentage = (acidentes/populacao) * 100)
  131. percent_est15 <- inner_join(acd15, est15, by = c("uf", "municipio")) %>%
  132. select(uf, mesorregiao, acidentes, populacao) %>%
  133. group_by(mesorregiao) %>%
  134. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  135. mutate(percentage = (acidentes/populacao) * 100)
  136. percent_est16 <- inner_join(acd16, est16, by = c("uf", "municipio")) %>%
  137. select(uf, mesorregiao, acidentes, populacao) %>%
  138. group_by(mesorregiao) %>%
  139. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  140. mutate(percentage = (acidentes/populacao) * 100)
  141. percent_est17 <- inner_join(acd17, est17, by = c("uf", "municipio")) %>%
  142. select(uf, mesorregiao, acidentes, populacao) %>%
  143. group_by(mesorregiao) %>%
  144. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  145. mutate(percentage = (acidentes/populacao) * 100)
  146. print("Writing csv files...")
  147. write_delim(percent_est12, "../app/data/percentage_meso2012.csv", delim = ";")
  148. write_delim(percent_est13, "../app/data/percentage_meso2013.csv", delim = ";")
  149. write_delim(percent_est14, "../app/data/percentage_meso2014.csv", delim = ";")
  150. write_delim(percent_est15, "../app/data/percentage_meso2015.csv", delim = ";")
  151. write_delim(percent_est16, "../app/data/percentage_meso2016.csv", delim = ";")
  152. write_delim(percent_est17, "../app/data/percentage_meso2017.csv", delim = ";")
  153. print("Writing rds file...")
  154. saveRDS(percent_est12, "../app/data/rds/percentage_meso2012.rds")
  155. saveRDS(percent_est13, "../app/data/rds/percentage_meso2013.rds")
  156. saveRDS(percent_est14, "../app/data/rds/percentage_meso2014.rds")
  157. saveRDS(percent_est15, "../app/data/rds/percentage_meso2015.rds")
  158. saveRDS(percent_est16, "../app/data/rds/percentage_meso2016.rds")
  159. saveRDS(percent_est17, "../app/data/rds/percentage_meso2017.rds")
  160. print("Finishing gracefully!")