1

I'm trying to plot certain medical facilities within a 25-mile radius of a certain geocoded point. :

Dataset of facilities looks like this:

Name        Lat          Long        Type        Color
A          42.09336    -76.79659      X          green
B          43.75840    -74.25250      X          green
C          43.16816    -77.60332      Y          blue

...

The list of facilities, however, spans all across the country (USA), but I only want to plot the facilities that are present within the circle. The center of the buffer circle is the set of coordinates (long =-73.857932, lat = 41.514096) and radius 25 miles.

So in the dataset that I would need to plot, I need to filter the list of facilities, their latitude and longitude, type and color

I'm really new at this and running a tight deadline so if someone could explain that would be great.

PS: I also want to count the type of facility (but I guess that would be a simple dplyr %>% n() once the filter is created, right?)

  • 1
    Possible duplicate of [find locations within certain lat/lon distance in r](https://stackoverflow.com/questions/39222302/find-locations-within-certain-lat-lon-distance-in-r) – bouncyball Jan 07 '19 at 21:47

1 Answers1

1

You can use the function distHaversine from the geosphere package (assuming df is your dataframe and 43, -77 are the coordinates of your reference point):

geosphere::distHaversine(c(43,  -77), df[, 2:3]) / 1609.334 <= 25
#[1]  TRUE FALSE FALSE

The default output will be meters, so the division by 1609.334 will convert to miles.

dave-edison
  • 3,666
  • 7
  • 19
  • Thanks!! Didn't realize it was that simple, I was trying to use gBuffer which was jus too tedious to work with (sorry for the late reply) – Anurag Kaushik Jan 17 '19 at 19:42