Learn R Programming

tigris (version 2.1)

geo_join: Easily merge a data frame to a spatial data frame

Description

This function should be considered deprecated. Please update your workflow to use sf objects and dplyr's *_join() family of functions instead.

Usage

geo_join(spatial_data, data_frame, by_sp, by_df, by = NULL, how = "left")

Value

a joined spatial/data frame object

Arguments

spatial_data

A spatial data frame to which you want to merge data.

data_frame

A regular data frame that you want to merge to your spatial data.

by_sp

The column name you'll use for the merge from your spatial data frame.

by_df

The column name you'll use for the merge from your regular data frame.

by

(optional) If a named argument is supplied to the by parameter, geo_join will assume that the join columns in the spatial data and data frame share the same name.

how

The type of join you'd like to perform. The default, 'left', keeps all rows in the spatial data frame, and returns NA for unmatched rows. The alternative, 'inner', retains only those rows in the spatial data frame that match rows from the target data frame.

Examples

Run this code
if (FALSE) {

library(rnaturalearth)
library(WDI)
library(tigris)

dat <- WDI(country = "all", indicator = "SP.DYN.LE00.IN", start = 2012, end = 2012)

dat$SP.DYN.LE00.IN <- round(dat$SP.DYN.LE00.IN, 1)

countries <- ne_countries()

countries2 <- geo_join(countries, dat, 'iso_a2', 'iso2c')

nrow(countries2)

## [1] 177

countries3 <- geo_join(countries, dat, 'iso_a2', 'iso2c', how = 'inner')

nrow(countries3)

## [1] 169

}

Run the code above in your browser using DataLab