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

Asked

Viewed 98 times

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)

1 answer

2


You can save the data frames in a list.

Your function 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)))
    }
  }
  • 1

    It is inaccessible in the same way the list, but thank you Rafael, it was good to try, I figured out how to, just put : read_fwf(file,fwf_widths(vector_fixed width_fixed,vector_variables,envir = . Globalenv) in this way he copies the function environment to the global, I thank him once again.

  • Beauty then. Put this discovery as an answer and in two days (if I’m not mistaken), accept it.

  • I already did, thank you very much!

  • my comment was in order that you put your discovery as a new answer, and not accept mine, since it did not have an effect

Browser other questions tagged

You are not signed in. Login or sign up in order to post.