summary.R 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 %>% mutate(final = municipio, acidentes_final = acidentes_municipio)
  60. write_delim(acidentes, "../app/data/acidentes.csv", delim = ";")
  61. #Summarization by ds_tipo_local_acidente(axis) and ano_cat(draw lines) for radarchart
  62. #by_local <- data %>% group_by(ano_cat, ds_tipo_local_acidente) %>% summarize(acidentes = n())
  63. #b2012 <- by_local %>% filter(ano_cat == 2012) %>%
  64. # mutate(total = sum(acidentes), porcentagem = acidentes / total)
  65. #b2013 <- by_local %>% filter(ano_cat == 2013) %>%
  66. # mutate(total = sum(acidentes), porcentagem = acidentes / total)
  67. #b2014 <- by_local %>% filter(ano_cat == 2014) %>%
  68. # mutate(total = sum(acidentes), porcentagem = acidentes / total)
  69. #b2015 <- by_local %>% filter(ano_cat == 2015) %>%
  70. # mutate(total = sum(acidentes), porcentagem = acidentes / total)
  71. #b2016 <- by_local %>% filter(ano_cat == 2016) %>%
  72. # mutate(total = sum(acidentes), porcentagem = acidentes / total)
  73. #Summarization by ano_cat(axis) and sex(draw lines) for radarchart
  74. #Uncomment if necessary
  75. #by_sex <- data %>% group_by(ano_cat, cd_tipo_sexo_empregado_cat) %>% summarize(acidentes = n())
  76. #s2012 <- by_sex %>% filter(ano_cat == 2012) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)
  77. #s2013 <- by_sex %>% filter(ano_cat == 2013) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)
  78. #s2014 <- by_sex %>% filter(ano_cat == 2014) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)
  79. #s2015 <- by_sex %>% filter(ano_cat == 2015) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)
  80. #s2016 <- by_sex %>% filter(ano_cat == 2016) %>% mutate(total = sum(acidentes), porcentagem = acidentes / total)
  81. #Sumarization of places by ano_cat and acidentes.
  82. by_uf_municipio <- complete %>% group_by(uf, municipio, ano_cat) %>% summarize(acidentes = n())
  83. write_delim(by_uf_municipio, "../app/data/uf-municipio.csv", delim = ";")