joinLonLat: Join by Longitude/Latitude

Description Usage Arguments Details Value Examples

View source: R/clean_data.R

Description

Append one data frame to another by nearest longitude and latitude

Usage

1
2
joinLonLat(x, y, f = NULL, lonname = "Longitude", latname = "Latitude",
  outname = "distance", inc_lonlat = TRUE, verbose = TRUE)

Arguments

x

data frame to be appended to; should be the larger of x and y

y

data frame to join onto x

f

function to define what column is added to x, see 'Details' and 'Examples'

lonname

name of the longitude column shared by both data frames

latname

name of the latitude column shared by both data frames

outname

name of the column added to x by f

inc_lonlat

logical; if TRUE, longitude and latitude co-ordinates from y will be added to x

verbose

logical; if TRUE, progress bars will be displayed as data sets are joining

Details

This function first finds the distance between two sets of co-ordinates, for each data frame, using the haversine distance function (from the geosphere package),

d = 2 r arcsin( √{ sin^2 ( (θ_2 - θ_1) / 2 ) + cos (θ_1) cos(θ_2) sin^2( (φ_2 - φ_1)/2 ) } ),

where (φ_1, θ_1) is the longitude and latitude of point 1, and (φ_2, θ_2) is the longitude and latitude of point 2, both in radians, and r is the radius of the Earth (6378137m). Once the distances are found, the smallest distances are calculated and those rows in the data frame y that are closest to each row in x are appended

The function f decides what column will be appended to the original data frame x, it takes two arguments (but both do not need to be used), where the first argument uses data frame x, and the second uses y.

Value

the original data frame x, with one appended column defined by f, and the longitudes/latitudes if inc_lonlats=TRUE.

Examples

1
2
3
4
5
6
coords1 = data.frame("Longitude" = c(-20, -25, -23, -40),
                     "Latitude" = c(10, 15, 23, 13))
coords2 = data.frame("Longitude" = c(-17, -15, -26),
                "Latitude"= c(11, 31, 42),
                "Names" = c("Frasier", "Niles", "Martin"))
joinLonLat(coords1, coords2, f = function(z1, z2) z2$Names, outname="Closest_Names")

jakespiteri/chicagocrime documentation built on Jan. 24, 2020, 1:26 a.m.