choropleth-uf.R 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. print("Loading estimation of population files...")
  32. est2012 <- read_csv2("../app/data/estimativa_municipio_2012.csv",
  33. na = "NA",
  34. col_types = cols(
  35. uf = col_character(),
  36. municipio = col_character(),
  37. populacao = col_integer(),
  38. ano = col_integer()
  39. ))
  40. est2013 <- read_csv2("../app/data/estimativa_municipio_2013.csv",
  41. na = "NA",
  42. col_types = cols(
  43. uf = col_character(),
  44. municipio = col_character(),
  45. populacao = col_integer(),
  46. ano = col_integer()
  47. ))
  48. est2014 <- read_csv2("../app/data/estimativa_municipio_2014.csv",
  49. na = "NA",
  50. col_types = cols(
  51. uf = col_character(),
  52. municipio = col_character(),
  53. populacao = col_integer(),
  54. ano = col_integer()
  55. ))
  56. est2015 <- read_csv2("../app/data/estimativa_municipio_2015.csv",
  57. na = "NA",
  58. col_types = cols(
  59. uf = col_character(),
  60. municipio = col_character(),
  61. populacao = col_integer(),
  62. ano = col_integer()
  63. ))
  64. est2016 <- read_csv2("../app/data/estimativa_municipio_2016.csv",
  65. na = "NA",
  66. col_types = cols(
  67. uf = col_character(),
  68. municipio = col_character(),
  69. populacao = col_integer(),
  70. ano = col_integer()
  71. ))
  72. est2017 <- read_csv2("../app/data/estimativa_municipio_2017.csv",
  73. na = "NA",
  74. col_types = cols(
  75. uf = col_character(),
  76. municipio = col_character(),
  77. populacao = col_integer(),
  78. ano = col_integer()
  79. ))
  80. #Summarise population
  81. print("Summarising population...")
  82. est12 <- est2012 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
  83. est13 <- est2013 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
  84. est14 <- est2014 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
  85. est15 <- est2015 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
  86. est16 <- est2016 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
  87. est17 <- est2017 %>% group_by(uf) %>% summarise(populacao = sum(populacao))
  88. print("Summarising accidents...")
  89. acd12 <- complete %>% filter(ano_cat == 2012) %>%
  90. group_by(uf) %>%
  91. summarise(acidentes = n())
  92. acd13 <- complete %>% filter(ano_cat == 2013) %>%
  93. group_by(uf) %>%
  94. summarise(acidentes = n())
  95. acd14 <- complete %>% filter(ano_cat == 2014) %>%
  96. group_by(uf) %>%
  97. summarise(acidentes = n())
  98. acd15 <- complete %>% filter(ano_cat == 2015) %>%
  99. group_by(uf) %>%
  100. summarise(acidentes = n())
  101. acd16 <- complete %>% filter(ano_cat == 2016) %>%
  102. group_by(uf) %>%
  103. summarise(acidentes = n())
  104. acd17 <- complete %>% filter(ano_cat == 2017) %>%
  105. group_by(uf) %>%
  106. summarise(acidentes = n())
  107. #Join estimations tables and c
  108. print("Joining tables...")
  109. percent_UF_est12 <- inner_join(acd12, est12, by = c("uf")) %>%
  110. select(uf, acidentes, populacao) %>%
  111. group_by(uf) %>%
  112. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  113. mutate(percentage = (acidentes/populacao) * 100)
  114. percent_UF_est13 <- inner_join(acd13, est13, by = c("uf")) %>%
  115. select(uf, acidentes, populacao) %>%
  116. group_by(uf) %>%
  117. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  118. mutate(percentage = (acidentes/populacao) * 100)
  119. percent_UF_est14 <- inner_join(acd14, est14, by = c("uf")) %>%
  120. select(uf, acidentes, populacao) %>%
  121. group_by(uf) %>%
  122. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  123. mutate(percentage = (acidentes/populacao) * 100)
  124. percent_UF_est15 <- inner_join(acd15, est15, by = c("uf")) %>%
  125. select(uf, acidentes, populacao) %>%
  126. group_by(uf) %>%
  127. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  128. mutate(percentage = (acidentes/populacao) * 100)
  129. percent_UF_est16 <- inner_join(acd16, est16, by = c("uf")) %>%
  130. select(uf, acidentes, populacao) %>%
  131. group_by(uf) %>%
  132. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  133. mutate(percentage = (acidentes/populacao) * 100)
  134. percent_UF_est17 <- inner_join(acd17, est17, by = c("uf")) %>%
  135. select(uf, acidentes, populacao) %>%
  136. group_by(uf) %>%
  137. summarise(acidentes = sum(acidentes), populacao = sum(populacao)) %>%
  138. mutate(percentage = (acidentes/populacao) * 100)
  139. print("Writing csv files...")
  140. write_delim(percent_UF_est12, "../app/data/percentage_UF2012.csv", delim = ";")
  141. write_delim(percent_UF_est13, "../app/data/percentage_UF2013.csv", delim = ";")
  142. write_delim(percent_UF_est14, "../app/data/percentage_UF2014.csv", delim = ";")
  143. write_delim(percent_UF_est15, "../app/data/percentage_UF2015.csv", delim = ";")
  144. write_delim(percent_UF_est16, "../app/data/percentage_UF2016.csv", delim = ";")
  145. write_delim(percent_UF_est17, "../app/data/percentage_UF2017.csv", delim = ";")
  146. print("Writing rds file...")
  147. saveRDS(percent_UF_est12, "../app/data/rds/percentage_UF2012.rds")
  148. saveRDS(percent_UF_est13, "../app/data/rds/percentage_UF2013.rds")
  149. saveRDS(percent_UF_est14, "../app/data/rds/percentage_UF2014.rds")
  150. saveRDS(percent_UF_est15, "../app/data/rds/percentage_UF2015.rds")
  151. saveRDS(percent_UF_est16, "../app/data/rds/percentage_UF2016.rds")
  152. saveRDS(percent_UF_est17, "../app/data/rds/percentage_UF2017.rds")
  153. print("Finishing gracefully!")