Reads a dataset downloaded from the NHGIS extract system. Relies on csv files (with or without the extra header row).
read_nhgis( data_file, data_layer = NULL, verbose = TRUE, var_attrs = c("val_labels", "var_label", "var_desc") ) read_nhgis_sf( data_file, shape_file, data_layer = NULL, shape_layer = data_layer, shape_encoding = "latin1", verbose = TRUE, var_attrs = c("val_labels", "var_label", "var_desc") ) read_nhgis_sp( data_file, shape_file, data_layer = NULL, shape_layer = data_layer, shape_encoding = "latin1", verbose = TRUE, var_attrs = c("val_labels", "var_label", "var_desc") )
data_file | Filepath to the data (either the .zip file directly downloaded from the website, the path to the unzipped folder, or the path to the unzipped .csv file directly). |
---|---|
data_layer | For .zip extracts with multiple datasets, the name of the
data to load. Accepts a character vector specifying the file name, or
|
verbose | Logical, indicating whether to print progress information to console. |
var_attrs | Variable attributes to add from the codebook, defaults to
adding all (val_labels, var_label and var_desc). See
|
shape_file | Filepath to the shape files (either the .zip file directly downloaded from the website, or the path to the unzipped folder, or the unzipped .shp file directly). |
shape_layer | (Defaults to using the same value as data_layer) Specification
of which shape files to load using the same semantics as |
shape_encoding | The text encoding to use when reading the shape file. Typically the defaults should read the data correctly, but for some extracts you may need to set them manually, but if funny characters appear in your data, you may need to. Defaults to "latin1" for NHGIS. |
read_nhgis
returns a tbl_df
with only the tabular data,
read_nhgis_sf
returns a sf
object with data and the shapes, and
read_nhgis_sp
returns a SpatialPolygonsDataFrame
with data and
shapes.
Other ipums_read:
read_ipums_micro_chunked()
,
read_ipums_micro_yield()
,
read_ipums_micro()
,
read_ipums_sf()
,
read_terra_area()
,
read_terra_micro()
,
read_terra_raster()
csv_file <- ipums_example("nhgis0008_csv.zip") shape_file <- ipums_example("nhgis0008_shape_small.zip") data_only <- read_nhgis(csv_file) #> Use of data from NHGIS is subject to conditions including that users should #> cite the data appropriately. Use command `ipums_conditions()` for more details. #> #> #> Reading data file... # If sf package is availble, can load as sf object if (require(sf)) { sf_data <- read_nhgis_sf(csv_file, shape_file) } #> Use of data from NHGIS is subject to conditions including that users should #> cite the data appropriately. Use command `ipums_conditions()` for more details. #> #> #> Reading data file... #> Reading geography... #> options: ENCODING=latin1 #> Reading layer `US_pmsa_1990' from data source #> `C:\Users\derek\AppData\Local\Temp\RtmpKMS8Fr\filec783b73370\US_pmsa_1990.shp' #> using driver `ESRI Shapefile' #> Simple feature collection with 71 features and 8 fields #> Geometry type: MULTIPOLYGON #> Dimension: XY #> Bounding box: xmin: -2336182 ymin: -1247086 xmax: 2075339 ymax: 1476544 #> Projected CRS: Albers # If sp package is available, can load as SpatialPolygonsDataFrame if (require(rgdal) && require(sp)) { sp_data <- read_nhgis_sp(csv_file, shape_file) } #> Use of data from NHGIS is subject to conditions including that users should #> cite the data appropriately. Use command `ipums_conditions()` for more details. #> #> #> Reading data file... #> Reading geography... #> OGR data source with driver: ESRI Shapefile #> Source: "C:\Users\derek\AppData\Local\Temp\RtmpKMS8Fr\filec7811044a3", layer: "US_pmsa_1990" #> with 71 features #> It has 8 fields