2
I would like to upload the 2010 Census files. First I am loading the households, through a function, using the command read_fwf
, package readr
. However, the data frames created, stay within the environment of my function and I would like to externalize them, so that you have access in my environment outside the function. This is the job I wrote:
require(readr)
vetor_largura_domicilio=c(2,5,13,8,16,1,2,3,2,1,2,2,1,6,9,1,2,3,2,3,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,7,10,8,9, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
vetor_nome_variaveis_domicilio=c("V0001","V0002","V0011","V0300","V0010","V1001","V1002","V1003","V1004","V1006","V4001",
"V4002","V0201","V2011","V2012","V0202","V0203","V6203","V0204","V6204","V0205","V0206", "V0207","V0208","V0209","V0210","V0211","V0212","V0213","V0214","V0215","V0216","V0217", "V0218","V0219","V0220","V0221","V0222","V0301","V0401","V0402","V0701","V6529","V6530", "V6531","V6532","V6600","V6210","M0201","M2011","M0202","M0203","M0204","M0205","M0206", "M0207","M0208","M0209","M0210","M0211","M0212","M0213","M0214","M0215","M0216","M0217", "M0218","M0219","M0220","M0221","M0222","M0301","M0401","M0402","M0701","V1005")
dir_domicilio="C:/Users/Thiago.marques.censo/Desktop/Thiago/Microdados_Censo_2010/DOMICILIO_CENSO2010"
funcao_carga=function(diretorio,vetor_largura_fixa,vetor_variaveis) {
setwd(diretorio)
path <- diretorio files <- list.files(path=path, pattern="Amostra_Domicilios_11.txt")
for(file in files){
perpos <- which(strsplit(file, "")[[1]]==".")
assign(
gsub(" ","",substr(file, 1, perpos-1)),
read_fwf(file,fwf_widths(vetor_largura_fixa,vetor_variaveis)))
}
}
funcao_carga(dir_domicilio,vetor_largura_domicilio,vetor_nome_variaveis_domicilio)
Already took a look at the package microdataBrasil? In particular function
microdadosBrasil::read_CENSO()
. It might save you a lot of trouble.– Tomás Barcellos