0

I have a data frame that contains the longitude/latitude of various patients' city locations. I am curious about each patient's distance traveled to the hospital. Is there a way I can somehow find out the distance each patient traveled to the hospital with the geodist package?

Lets say the hospital is located here:

Latitude: 36.840

Longitude: -119.850

I want to calculate the distance between the hospital and where each patient lives. Here is my dataset:

> dput(d)
structure(list(major_city = c("Mountain View", "Watsonville", 
"Honolulu", "Los Altos", "Morgan Hill", "Fulton", "Oak Grove", 
"Port Kent", "Bedford", "San Jose"), latitude = c(37.39, 36.9, 
21.31, 37.36, 37.1, 43.3, 36.69, 44.526, NA, 37.27), longitude = c(-122.07, 
-121.7, -157.85, -122.15, -121.7, -76.4, -87.44, -73.409, NA, 
-121.84)), row.names = c(NA, -10L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7f98af80bae0>, index = integer(0))

I want to do this for the entire dataset rather than just one patient.

Jamie
  • 543
  • 7
  • 1
    Does these answers address your question? https://stackoverflow.com/questions/19847833/speeding-up-a-simple-for-loop-with-vectorization-in-r/19848003#19848003 and https://stackoverflow.com/questions/67477686/r-calculate-distance-in-miles-using-2-latitude-2-longitude-vectors-in-a-data-f/67479003#67479003 – Jon Spring Jan 25 '23 at 00:38

1 Answers1

1

You could try this. I used the geosphere package instead of geodist but should be good enough

df <- data.frame(major_city = c("Mountain View", "Watsonville", 
                              "Honolulu", "Los Altos", "Morgan Hill", "Fulton", "Oak Grove", 
                              "Port Kent", "Bedford", "San Jose"),
                  latitude = c(37.39, 36.9, 21.31, 37.36, 37.1, 43.3, 36.69, 44.526, NA, 37.27), 
                  longitude = c(-122.07, -121.7, -157.85, -122.15, -121.7, -76.4, -87.44, -73.409, NA, -121.84))




df$distance_m <- apply(df, MARGIN = 1, function(x) {
  geosphere::distGeo(as.numeric(c(x['longitude'], x['latitude'])),
                     c(-119.85, 36.84)) 
  })

df$distance_km <- df$distance_m / 1000

This is the output

      major_city latitude longitude distance_m distance_km
1  Mountain View   37.390  -122.070   206527.4    206.5274
2    Watsonville   36.900  -121.700   165083.2    165.0832
3       Honolulu   21.310  -157.850  4045683.3   4045.6833
4      Los Altos   37.360  -122.150   212440.1    212.4401
5    Morgan Hill   37.100  -121.700   167241.2    167.2412
6         Fulton   43.300   -76.400  3731976.3   3731.9763
7      Oak Grove   36.690   -87.440  2879678.0   2879.6780
8      Port Kent   44.526   -73.409  3962114.4   3962.1144
9        Bedford       NA        NA         NA          NA
10      San Jose   37.270  -121.840   183321.3    183.3213
clsantos
  • 26
  • 2