Description Usage Arguments Details Value Examples
Append one data frame to another by nearest longitude and latitude
1 2 | joinLonLat(x, y, f = NULL, lonname = "Longitude", latname = "Latitude",
outname = "distance", inc_lonlat = TRUE, verbose = TRUE)
|
x |
data frame to be appended to; should be the larger of |
y |
data frame to join onto |
f |
function to define what column is added to |
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 |
inc_lonlat |
logical; if |
verbose |
logical; if |
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
.
the original data frame x
, with one appended column defined by f
, and the longitudes/latitudes if inc_lonlats=TRUE
.
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")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.