This vignette will discuss how to use the dspgWork::save_acs function.

library(dspgWork)

The save_acs function accomplishes two tasks:

  1. Saves an ACS data set to a user-specified folder (specified with dataSavePath)

  2. Creates or adds to a .csv documentation file in a user-specified folder (specified via docPath)

Save a “raw” ACS data set

Suppose we are interested in downloading and saving Table B01001 corresponding to Sex by Age Group. The save_acs function will internally download the corresponding data set using tidycensus::get_acs if either the variables or table arguments are user-specified. The code below will download the B01001 table information for each county in Iowa. The downloaded data set will be saved in the dataSavePath folder (in the data_raw folder).

The other arguments of the save_acs function are for creating or adding to a documentation .csv file. Each argument will populate a column in the documentation file, although some will also be used in the saved file name.

dspgWork::save_acs(theme = "demographics",title = "sex by age",collector = "Joe",status = "Raw", #documentation arguments
                   geography = "county",state = "IA",table = "B01001",survey = "acs5", #tidycensus arguments
                   docPath = "data_raw/", #where to save documentation file
                   dataSavePath = "data_raw/") #where to save data set

The .csv documentation file, now saved as dataRaw_sexByAge_acs5_2019_documentation.csv in the data_raw folder, contains documentation of the above data set with a few fields missing. You, as the user, will need to populate the missing fields. You can also copy & paste the contents of this .csv files into other documentation files.

Save a processed data set in your R environment

Now suppose that you’ve already been working with a data set that you’re now interested in saving. For concreteness, we’ll continue working with the B01001 table downloaded in the previous section and perform some simple processing.

# table B01001 saved here:
load("../../data_raw/dataRaw_sexByAge_acs5_2019.rda")

head(dataRaw_sexByAge_acs5_2019)
#>   GEOID               NAME   variable estimate moe
#> 1 19001 Adair County, Iowa B01001_001     7085  NA
#> 2 19001 Adair County, Iowa B01001_002     3505  25
#> 3 19001 Adair County, Iowa B01001_003      213  12
#> 4 19001 Adair County, Iowa B01001_004      221  42
#> 5 19001 Adair County, Iowa B01001_005      213  42
#> 6 19001 Adair County, Iowa B01001_006      128  10

We will add the ACS variable labels to the dataRaw_sexByAge_acs5_2019 data frame. These labels are saved in the dataClean_variableLabels_acs5_2019 file saved in the data_clean folder, which we will also load.

load("../../data_clean/dataClean_variableLabels_acs5_2019.rda")

head(dataClean_variableLabels_acs5_2019)

Now we’ll join the labels to the data set. We will also remove the coming " County, Iowa" string in each county name.

dataClean_sexByAge_acs5_2019 <- dataRaw_sexByAge_acs5_2019 %>%
  dplyr::left_join(dataClean_variableLabels_acs5_2019,
                   by = c("variable" = "name")) %>%
  dplyr::mutate(NAME = stringr::str_remove(NAME," County, Iowa"))

head(dataClean_sexByAge_acs5_2019)

There are certainly other manipulations that we can perform on these data to make them “cleaner,” but let’s suppose that we’re ready to save this data set to the data_clean folder. The call to save_acs is very similar to the one above that saved the original, raw data set. The key difference will be to pass dataClean_sexByAge_acs5_2019 to the cleanDataFrame argument. The other argument changes are intended to reflect the fact that we are now dealing with a “clean” data set.

dspgWork::save_acs(theme = "demographics",title = "sex by age",collector = "Joe",status = "Clean", #documentation arguments
                   cleanDataFrame = dataClean_sexByAge_acs5_2019,
                   docPath = "../../data_clean/", #where to save documentation file
                   dataSavePath = "../../data_clean/") #where to save data set

The .csv documentation file, now saved as dataClean_sexByAge_acs5_2019_documentation.csv in the data_clean folder, contains documentation of the above data set with a few fields missing. You, as the user, will need to populate the missing fields. You can also copy & paste the contents of this .csv files into other documentation files.