[英]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.