This function finds the nearest lat/long pairs to another lat/long pair. So in the york building and york crime context, writing nearest(york_crime,york) reads as "find the nearest crime in york to each building in york, and returns a dataframe with every building in york, the nearest york_crime to each building, and the distance in metres between the two. Likewise, you could write nearest(york, york_crime), and this would return the nearest building to every crime. nearest assumes that the names of the latitude and longitude are "lat" and "long", but you can provide these names.

nearest(nearest_df, to_df, nearest_lat = "lat", nearest_long = "long",
  to_lat = "lat", to_long = "long")

Arguments

nearest_df

a dataframe containing latitude and longitude

to_df

a dataframe containing latitude and longitude

nearest_lat

name of latitude in nearest_df

nearest_long

name of longitude in nearest_df

to_lat

name of latitude in to_df

to_long

name of longitude in to_df

Value

dataframe of "to_df" along with the nearest "nearest_df" to each row, along with the distance between the two, and the nearest_id, the row position of the nearest_df closest to that row.

Examples

library(maxcovr) nearest(nearest_df = york_crime, to_df = york)
#> # A tibble: 2,944 x 22 #> to_id nearest_id distance long_to lat_to object_id desig_id pref_ref name #> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <chr> <int> <chr> #> 1 1 33 36.0 -1.09 54.0 6144 DYO1195 463280 GUIL… #> 2 2 183 4.30 -1.09 54.0 6143 DYO1746 NA Boot… #> 3 3 183 35.8 -1.09 54.0 6142 DYO1373 462942 BOOT… #> 4 4 183 89.1 -1.08 54.0 6141 DYO1745 NA Boot… #> 5 5 942 236. -1.10 53.9 6140 DYO14 325952 CHUR… #> 6 6 942 236. -1.10 53.9 6139 DYO14 325952 CHUR… #> 7 7 245 60.6 -1.08 54.0 6138 DYO1226 463228 NUMB… #> 8 8 264 60.9 -1.08 54.0 6137 DYO1226 463228 NUMB… #> 9 9 1106 91.8 -1.08 54.0 6136 DYO421 464726 TERR… #> 10 10 184 23.9 -1.08 54.0 6135 DYO978 463684 NA #> # … with 2,934 more rows, and 13 more variables: grade <chr>, category <chr>, #> # persistent_id <chr>, date <chr>, lat_nearest <dbl>, long_nearest <dbl>, #> # street_id <chr>, street_name <chr>, context <chr>, id <chr>, #> # location_type <chr>, location_subtype <chr>, outcome_status <chr>
# you can use the pipe as well
# NOT RUN { library(magrittr) york_crime %>% nearest(york) # }