summary.R 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env Rscript
  2. library(tidyverse)
  3. brasil <- read_csv2("../app/data/brasil.csv")
  4. data <- read_csv2("../app/data/dados_cat.csv", na = "NA",
  5. col_types = cols(
  6. st_acidente_feriado = col_character(),
  7. ds_agente_causador = col_character(),
  8. ano_cat = col_integer(),
  9. ds_cnae_classe_cat = col_character(),
  10. dt_acidente = col_date(format = "%d/%m/%Y"),
  11. st_dia_semana_acidente = col_character(),
  12. ds_emitente_cat = col_character(),
  13. hora_acidente = col_time(format = "%H%M"),
  14. idade_cat = col_integer(),
  15. cd_indica_obito = col_character(),
  16. nm_municipio = col_character(),
  17. nome_uf = col_character(),
  18. ds_natureza_lesao = col_character(),
  19. ds_cbo = col_character(),
  20. ds_parte_corpo_atingida = col_character(),
  21. cd_tipo_sexo_empregado_cat = col_character(),
  22. ds_tipo_acidente = col_character(),
  23. ds_tipo_local_acidente = col_character()
  24. ))
  25. # Use better variable names for dataset and put locality data in front
  26. data <- rename(data, uf = nome_uf,
  27. municipio = nm_municipio) %>%
  28. select(uf, municipio, everything())
  29. # Add correponding locality data from brasil to data
  30. complete <- brasil %>% inner_join(data, by = c("uf", "municipio"))
  31. write_delim(complete, "../app/data/completo.csv", delim = ";")
  32. # Number of accidents:
  33. country <- group_by(complete, pais) %>% summarize(acidentes = n())
  34. by_region <- group_by(complete, regiao) %>% summarize(acidentes = n())
  35. by_uf <- group_by(complete, uf) %>% summarize(acidentes = n())
  36. by_meso <- group_by(complete, mesorregiao) %>% summarize(acidentes = n())
  37. by_micro <- group_by(complete, microrregiao) %>% summarize(acidentes = n())
  38. by_town <- group_by(complete, uf, municipio) %>% summarize(acidentes = n())
  39. # Put every accident alongside locality (this is temporary)
  40. acidentes <- brasil %>% inner_join(country, by = c("pais")) %>%
  41. inner_join(by_region, by = c("regiao")) %>%
  42. inner_join(by_uf, by = c("uf")) %>%
  43. inner_join(by_meso, by = c("mesorregiao")) %>%
  44. inner_join(by_micro, by = c("microrregiao")) %>%
  45. inner_join(by_town, by = c("uf", "municipio")) %>%
  46. select(pais,
  47. total = acidentes.x,
  48. regiao,
  49. acidentes_regiao = acidentes.y,
  50. uf,
  51. acidentes_uf = acidentes.x.x,
  52. mesorregiao,
  53. acidentes_meso = acidentes.y.y,
  54. microrregiao,
  55. acidentes_micro = acidentes.x.x.x,
  56. municipio,
  57. acidentes_municipio = acidentes.y.y.y)
  58. # Repete last two columns so we can inspect a single town on parallel sets
  59. acidentes <- acidentes %>%
  60. mutate(final = municipio, acidentes_final = acidentes_municipio)
  61. write_delim(acidentes, "../app/data/acidentes.csv", delim = ";")