0

I'm doing an analysis where I want to see which of the hospitals in my dataset is closet to the nearest nursing home. So for each nursing home I my goal is to find the hospital within the shortest distance.

I have used st_distance to calculate this but I would like to see the actual names of the Origin and Destination and not just the calculated distance.

### Calculating the distance between Nursing Homes and Hospitals ###

#distance from nursing home TO hospitals

    distances <- st_distance(nursing_health, hospitals) * 0.000621371    
                 #converting meters to miles

    #finding the distance to the closest hospital for each RHF

nursing_health <- nursing_health %>%
    mutate(distance_to_hospital = as.vector(apply(distances, 1, min)))

Is there a way I can actually see the names of which hospitals have the closest distance to a given nursing home?

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
  • 1
    your code is not exactly reproducible, but I believe you can be helped by setting rownames and colnames to the distance matrix; see this older answer: https://stackoverflow.com/a/69074385/7756889 – Jindra Lacko May 15 '23 at 12:56
  • you're likely looking for a combination of ```st_join()``` and ```st_nearest_feature``` – maarvd May 15 '23 at 13:05
  • Instead of `min` you could use `which.min` to get an index, you can extract minimum distance value and correct name through this. – margusl May 15 '23 at 14:48
  • And you can convert your distance matrix to miles with `units::set_units(distances, miles)` .Or if you want to convert manually, you probably should drop units as well as for R your distances are still in meters. – margusl May 15 '23 at 14:57

0 Answers0