ropensci / eph

Herramientas para procesamiento de la base usuaria de la EPH
https://ropensci.github.io/eph/
Other
58 stars 18 forks source link

función 'descarga_base_eph' -multiples bases #8

Closed DiegoKoz closed 4 years ago

DiegoKoz commented 5 years ago

Que la función permita definir un rango de años y trimestres para descargar y realice el correspondiente binding de las bases

DiegoKoz commented 5 years ago

Un comienzo


get_multiple_bases <- function(year = 2016:2018,
                              trimester = 2:4,
                              wave = NA,
                              type='individual'){

  base <- tibble::tibble()
  if (!is.na(trimester)) {
    for (y in year) {
      for (trim in trimester) {
        tmp <- get_microdata(year = y,trimester = trim)
        names(tmp) <- toupper(names(tmp))

        if (!purrr::is_empty(base)) {
          types_base <- unlist(map(base, typeof))
          types_tmp <- unlist(map(tmp, typeof))
          diferent_types <- tibble::tibble(col=names(types_base),types_base) %>%
            left_join(tibble(col=names(types_tmp),types_tmp), by = "col") %>%
            dplyr::filter(types_base == 'character' & types_tmp=='integer')
          tmp <- dplyr::mutate_at(tmp,diferent_types$col,as.character)
        }

        base <- dplyr::bind_rows(base,tmp)
      }
    }
  }
  if (!is.na(wave)) {
    for (y in year) {
      for (w in wave) {
        tmp <- get_microdata(year = y,wave =  w)
        names(tmp) <- toupper(names(tmp))

        if (!purrr::is_empty(base)) {
          types_base <- unlist(map(base, typeof))
          types_tmp <- unlist(map(tmp, typeof))
          diferent_types <- tibble::tibble(col=names(types_base),types_base) %>%
            left_join(tibble(col=names(types_tmp),types_tmp), by = "col") %>%
            dplyr::filter(types_base == 'character' & types_tmp=='integer')
          tmp <- dplyr::mutate_at(tmp,diferent_types$col,as.character)
        }

        base <- dplyr::bind_rows(base,tmp)
      }
    }
  }
}