I want to merge city names to approximate coordinates.
I have two datasets.
- lat-long for cities, called
cities
. - lat-long for observed events, called
events
.
Most of the events occur just out-side the lat-longs of the city.
I want to merge in the city
from cities
if the lat-long are max 1 lat
and lon
different from those listed in events
.
The nearest
function in data.table
seems to be too crude.
What would you do? Use maptools
?
Example:
cities <- data.table(city = c("A", "B", "C"),
lat = c(23.4, 43.5, 21.3),
lon = c(100, 98.4, -78.2))
events <- data.table(event = c("X1", "Y1", "B1"),
lat = c(24.4, 42.5, 23.3),
lon = c(101, 100.4, -78.2)))
result <- data.table(event = c("X1", "Y1", "B1"),
lat = c(23.4, 43.5, 21.3),
lon = c(100, 98.4, -78.2),
city = c("A", NA, NA))
> result
event lat lon city
1: X1 23.4 100.0 A
2: Y1 43.5 98.4 <NA>
3: B1 21.3 -78.2 <NA>