# Efficient method for integrating multiple tables into a single data frame in r

**URL:** <https://forum.ipums.org/t/efficient-method-for-integrating-multiple-tables-into-a-single-data-frame-in-r/3218>\
**Category:** NHGIS\
**Created:** [October 27, 2019, 1:16am UTC](https://forum.ipums.org/t/efficient-method-for-integrating-multiple-tables-into-a-single-data-frame-in-r/3218 "2019-10-27T01:16:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Arent](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@Ken\_Arent](https://forum.ipums.org/u/Ken_Arent)\
**Post date:** [October 27, 2019, 1:16am UTC](https://forum.ipums.org/t/efficient-method-for-integrating-multiple-tables-into-a-single-data-frame-in-r/3218/1 "2019-10-27T01:16:01Z")

</div>

I am trying to use the read\_nhgis\_sf to generate a single data frame in r.

Assume my zip file is:

nhgis0003\_csv.zip

And it contains:

nhgis0003\_ds120\_1990\_tract.csv

nhgis0003\_ds120\_1990\_county\_codebook.txt

nhgis0003\_ds123\_1990\_tract.csv

nhgis0003\_ds123\_1990\_tract\_codebook.txt

How do I set up the data\_layer command so that it integrates both tables into a single frame?

read\_nhgis\_sf(data\_file, shape\_file, data\_layer = NULL,  
shape\_layer = data\_layer, shape\_encoding = “latin1”,  
verbose = TRUE, var\_attrs = [c](https://www.rdocumentation.org/packages/base/topics/c)(“val\_labels”, “var\_label”, “var\_desc”))

Can I integrate the codebooks as well?

Thank You

---

<div class="post-metadata">

**Author:** ![JeffBloem](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.ipums.org/jeffbloem/32/40_2.png) [@JeffBloem](https://forum.ipums.org/u/JeffBloem)\
**Post date:** [November 4, 2019, 2:38pm UTC](https://forum.ipums.org/t/efficient-method-for-integrating-multiple-tables-into-a-single-data-frame-in-r/3218/2 "2019-11-04T14:38:00Z")

</div>

ipumsr does not include functionality to read multiple data layers at a time. However, the ipumsr functions `read_nhgis_sf()` and `read_nhgis()` return regular R data.frame objects, which can be combined in several ways that don’t rely on the ipumsr package. Thus the best approach would be to read the layers in one at a time and then combine them after reading. If the data layers all refer to the same geographic units, then you could use one of [dplyr’s join functions](https://dplyr.tidyverse.org/reference/join.html) to combine the data layers. If the data layers refer to different geographic units, but contain the same attributes for those units, then you could combine the data.frames with the [`bind_rows()`](https://dplyr.tidyverse.org/reference/bind.html) function.

ipumsr also does not include functionality to read multiple codebooks at a time. It’s not clear what it would mean to “integrate” the codebooks. The function `read_ipums_codebook()` returns a list of information, some of which pertains to the extract more broadly, and some of which pertains to a specific data layer. If you combined the data layer data.frames by joining as described above, it might make sense to combine the corresponding `var_info` data.frame objects that are one element of the list returned by `read_ipums_codebook()`. Here’s some example code:

```auto
library(dplyr)
library(ipumsr)
codebook_1 <- read_ipums_codebook(cb_file, data_layer = data_layer_1)
codebook_2 <- read_ipums_codebook(cb_file, data_layer = data_layer_2)
combined_var_info <- bind_rows(codebook_1$var_info, codebook_2$var_info) %>%
    distinct(var_name, .keep_all = TRUE)

```

If you’d like more help, we likely need more information about the contents of the various data layers you want to combine, and what you are hoping the combined object(s) will look like. Feel free to follow up via email at [ipums@umn.edu](mailto:ipums@umn.edu)
