Access a data frame object, within a function in R - Census 2010

2

I would like to upload the Census 2010 files. First, I am loading the domains through a function, using the read_fwf command, of the readr package. However, the data frames created are within the scope of my role and I would like to outsource them, so that I have access to my environment outside the role. Here is the function 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)
    
asked by anonymous 19.09.2017 / 15:04

1 answer

2

You can save the date frames in a list.

Your role would be:

funcao_carga=function(diretorio,vetor_largura_fixa,vetor_variaveis) {

  setwd(diretorio)
  lista <- list()

  path <- diretorio files <- list.files(path=path, pattern="Amostra_Domicilios_11.txt")

  for(file in files){
    perpos <- which(strsplit(file, "")[[1]]==".")

    lista[[file]] <- assign(
      gsub(" ","",substr(file, 1, perpos-1)), 
      read_fwf(file,fwf_widths(vetor_largura_fixa,vetor_variaveis)))
    }
  }
    
19.09.2017 / 15:35