rfsaldanha / microdatasus

Download de dados do DataSUS e pré-processamento no R.
https://rfsaldanha.github.io/microdatasus/
Other
230 stars 57 forks source link

Error in process_sinasc --- incompatible types (2005) #90

Closed nattrigo closed 10 months ago

nattrigo commented 10 months ago

Hi! For the year 2005, the function process_sinasc return an error:

df <- process_sinasc(raw_data_2005, municipality_data = TRUE) Error in dplyr::left_join(): ! Can't join x$CODOCUPMAE with y$CODOCUPMAE due to incompatible types. ℹ x$CODOCUPMAE is a . ℹ y$CODOCUPMAE is a . Run rlang::last_trace() to see where the error occurred.

Backtrace: ▆

  1. └─microdatasus::process_sinasc(raw_data_2005, municipality_data = TRUE)
  2. ├─base::factor(...)
  3. ├─base::ifelse(...)
  4. ├─dplyr::left_join(data, tabOcupacao, by = "CODOCUPMAE")
  5. └─dplyr:::left_join.data.frame(data, tabOcupacao, by = "CODOCUPMAE")

I believe the problem is process_sinasc.R in this section (lines 102-117):

if ("CODOCUPMAE" %in% variables_names) { if (!("DTNASC" %in% variables_names)) stop("The variable DTNASC is needed to preprocess the variable CODOCUPMAE") colnames(tabOcupacao)[1] <- "CODOCUPMAE" tabOcupacao$CODOCUPMAE = as.character(tabOcupacao$CODOCUPMAE) # add-nat colnames(tabCBO)[1] <- "CODOCUPMAE" tabCBO$CODOCUPMAE = as.character(tabCBO$CODOCUPMAE) ## add - nat ano <- lubridate::year(data$DTNASC) data$CODOCUPMAE <- factor(ifelse( ano <= 2005, dplyr::left_join(data, tabOcupacao, by = "CODOCUPMAE")$nome, dplyr::left_join(data, tabCBO, by = "CODOCUPMAE")$nome )) }

Here: colnames(tabOcupacao)[1] <- "CODOCUPMAE" colnames(tabCBO)[1] <- "CODOCUPMAE"

The type of one column is integer and the other is a character. I`ll suggest a pull request to fix for this (converting both columns to character).

thanks!

rfsaldanha commented 10 months ago

Thanks for the pull request! I will merge it soon.