简体   繁体   English

计算纬度和经度点之间的距离?

[英]Calculate distance between latitude and longitude points?

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?有没有办法可以找出每位患者使用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.我想对整个数据集执行此操作,而不仅仅是一个患者。

You could try this.你可以试试这个。 I used the geosphere package instead of geodist but should be good enough我使用 geosphere package 而不是 geodist 但应该足够好了

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这是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算多个经纬度点之间的距离 - Calculate distance between multiple latitude and longitude points 获取经度点和纬度点向量之间的距离 - Getting distance between vectors of longitude and latitude points 经度/纬度点之间的最大距离 - Greatest distance between set of longitude/latitude points 如何在短时间内计算一个数据集中的经纬度点与另一个数据集中的经纬度点之间的最短距离 - How to calculate shortest distance between longitude-latitude points in one dataset with those in another in a short time 如何计算多个经纬度数据之间的距离? - How can I calculate distance between multiple latitude and longitude data? 在 R 中查找两组点(纬度和经度)点之间的最短距离 - Finding shortest distance between two sets of points ( latitude and longitude) points in R 计算数据帧R中多个距离经度纬度 - Calculate distance longitude latitude of multiple in dataframe R R代码计算多个经纬度对之间的距离并提取最接近的对 - R code to calculate distance between multiple latitude-longitude pairs and extract the closest pairs 在 BigQuery 中本地使用经度和纬度计算起点和目的地之间的行驶距离? - Calculate driving distance between origin and destination using longitude and latitude natively in BigQuery? 计算大型数据集的经度和纬度之间的距离 - Calculate distances between longitude and latitude for a large datasets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM