treemap_summary.R 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env Rscript
  2. library(tidyverse)
  3. library(lubridate)
  4. # Generate a summary considering current scope
  5. summarize_scope <- function(x) {
  6. summarize (x,
  7. cnae_secao = names(which.max(table(cnae_secao))),
  8. ds_natureza_lesao = names(which.max(table(ds_natureza_lesao))),
  9. cbo_grande_grupo = names(which.max(table(cbo_grande_grupo))),
  10. agrupamento_parte_do_corpo = names(which.max(table(agrupamento_parte_do_corpo))),
  11. ds_grupo_agcausadores = names(which.max(table(ds_grupo_agcausadores))),
  12. ds_tipo_acidente = names(which.max(table(ds_tipo_acidente))),
  13. ds_tipo_local_acidente = names(which.max(table(ds_tipo_local_acidente))),
  14. ds_emitente_cat = names(which.max(table(ds_emitente_cat))),
  15. idade = round(mean(idade_cat), 2),
  16. obito = round(sum(cd_indica_obito == "S")/(n()), 2),
  17. sexo = round(sum(cd_tipo_sexo_empregado_cat == "Masculino")/(n()), 2),
  18. turno = round(sum(turno == "Diurno")/(n()), 2)
  19. )
  20. }
  21. brasil <- read_csv2("../app/data/brasil.csv")
  22. complete <- read_csv2("../app/data/paracoord_data.csv")
  23. # Summarize for every scope:
  24. country <- group_by(complete, pais) %>% summarize_scope()
  25. by_region <- group_by(complete, pais, regiao) %>% summarize_scope()
  26. by_uf <- group_by(complete, regiao, uf) %>% summarize_scope()
  27. by_meso <- group_by(complete, uf, mesorregiao) %>% summarize_scope()
  28. by_micro <- group_by(complete, mesorregiao, microrregiao) %>% summarize_scope()
  29. by_town <- group_by(complete, microrregiao, municipio) %>% summarize_scope()
  30. # We need full information for towns
  31. by_town <- brasil %>% inner_join(by_town, by = c("microrregiao", "municipio"))
  32. # Write summaries
  33. write_delim(country, "../app/data/treemap/sumario-pais.csv", delim = ";")
  34. write_delim(by_region, "../app/data/treemap/sumario-regiao.csv", delim = ";")
  35. write_delim(by_uf, "../app/data/treemap/sumario-uf.csv", delim = ";")
  36. write_delim(by_meso, "../app/data/treemap/sumario-meso.csv", delim = ";")
  37. write_delim(by_micro, "../app/data/treemap/sumario-micro.csv", delim = ";")
  38. write_delim(by_town, "../app/data/treemap/sumario-municipio.csv", delim = ";")