Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sinasc.dn #2

Open
silvadenisson opened this issue Nov 16, 2016 · 0 comments
Open

sinasc.dn #2

silvadenisson opened this issue Nov 16, 2016 · 0 comments
Assignees

Comments

@silvadenisson
Copy link

essa é a função sinasc.dn fiz a partir da outras funções, mas substituir datasus.init por tempdir.
A função é para coletar os micro dados do nascidos vivo.

sinasc.dn <- function(years) {

sinasc <- data.frame()

for(i in years) {
for (y in c('AC', 'AL', 'AP', 'AM',
'BA', 'CE', 'DF', 'ES',
'GO', 'MA', 'MT', 'MS',
'MG', 'PA', 'PB', 'PR',
'PE', 'PI', 'RJ', 'RN',
'RS', 'RO', 'RR', 'SC',
'SP', 'SE', 'TO')) {

# Prepare local file names and remote download paths

filenames  <- paste0("DN", y , i, ".dbc")

localnames <- file.path(tempdir(), filenames)

url.base   <- "ftp://ftp.datasus.gov.br/dissemin/publicos/SINASC/NOV/DNRES"

if( !file.exists(localnames) ) {
  # Try default path
  url <- paste(url.base, filenames, sep = "/" )
  
  if( download.file(url, destfile = localnames) ) {
    # Some of the files are postfixed as .DBC (uppercase), try again on error
    url <- paste(url.base, toupper(filenames), sep = "/" )
    download.file(url,  destfile = localnames)
  }
}

# Select which fields of the files will be loaded considerando nao 2000 como referencia
fields     <- c("NUMERODN",
                "LOCNASC",
                "CODMUNNASC",
                "IDADEMAE",
                "ESTCIVMAE",
                "ESCMAE",
                "QTDFILVIVO",
                "QTDFILMORT",
                "CODMUNRES",
                "GESTACAO",
                "GRAVIDEZ",
                "PARTO",
                "CONSULTAS",
                "DTNASC",
                "SEXO",
                "APGAR1",
                "APGAR5",
                "RACACOR",
                "PESO",
                "CODANOMAL"
)



# Load downloaded files into a data.frame
  df <- read.dbc::read.dbc(localnames, as.is = TRUE)
  
  # Select only applicable fields
  df <- df[, fields]
  
  # This column is used as an indicator of which file originated the data
  df$dataset <- filenames
  
  sinasc <- rbind(df, sinasc, make.row.names = FALSE)
}

}

Returns data.frame

sinasc
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants