-
-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Labels
Description
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
}
Reactions are currently unavailable