[英]Find Distance Between Locations in R Data Frame
I've got a dataframe with the following rows and columns and I'd like to find the distance between county_a and county_b 我有一个包含以下行和列的数据框,我想找到county_a和county_b之间的距离
county_a county_b lat_a lon_a winner_a lat_b lon_b winner_b
1 01001 01001 32.536382 -86.644490 rep 32.536382 -86.644490 rep
2 01003 01001 30.659218 -87.746067 rep 32.536382 -86.644490 rep
3 01005 01001 31.870670 -85.405456 rep 32.536382 -86.644490 rep
4 01007 01001 33.015893 -87.127148 rep 32.536382 -86.644490 rep
5 01009 01001 33.977448 -86.567246 rep 32.536382 -86.644490 rep
6 01011 01001 32.101759 -85.717261 dem 32.536382 -86.644490 rep
I've tried the following and got an error (both below): 我尝试了以下操作,但出现错误(均在下面):
library(geosphere)
library(RJDBC) # Not sure this was used for this but it comes up earlier in the program
library(dplyr)
df%>%mutate(dist = distm(c(lon_a,lat_a), c(lon_b, lat_b), fun=distHaversine))
error: Error in eval(substitute(expr), envir, enclos) : Wrong length for a vector, should be 2
错误:
Error in eval(substitute(expr), envir, enclos) : Wrong length for a vector, should be 2
Thanks in advance for the help! 先谢谢您的帮助!
If you can't figure out how to use a canned function from your R package, you could always define your own Haversine formula: 如果您不知道如何使用R包中的固定函数,则可以始终定义自己的Haversine公式:
gcd.slc <- function(long1, lat1, long2, lat2) {
R <- 6371 # Earth mean radius [km]
d <- acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(long2-long1)) * R
return(d) # Distance in km
}
This function uses the spherical law of cosines to find the distance between two points using their latitudes and longitudes. 此函数使用余弦的球面定律,根据其纬度和经度找出两点之间的距离。
Reference: https://www.google.com.sg/amp/s/www.r-bloggers.com/great-circle-distance-calculations-in-r/amp/?client=ms-android-samsung 参考: https : //www.google.com.sg/amp/s/www.r-bloggers.com/great-circle-distance-calculations-in-r/amp/?client=ms-android-samsung
You need to give the arguments in matrix form with two columns each. 您需要以矩阵形式给出参数,每个矩阵有两列。 So use
cbind
instead of c
: 因此,请使用
cbind
而非c
:
df <- read.table(text=" county_a county_b lat_a lon_a winner_a lat_b lon_b winner_b
1 01001 01001 32.536382 -86.644490 rep 32.536382 -86.644490 rep
2 01003 01001 30.659218 -87.746067 rep 32.536382 -86.644490 rep
3 01005 01001 31.870670 -85.405456 rep 32.536382 -86.644490 rep
4 01007 01001 33.015893 -87.127148 rep 32.536382 -86.644490 rep
5 01009 01001 33.977448 -86.567246 rep 32.536382 -86.644490 rep
6 01011 01001 32.101759 -85.717261 dem 32.536382 -86.644490 rep")
library(dplyr)
library(geosphere)
df %>% mutate(dist = distHaversine(cbind(lon_a, lat_a), cbind(lon_b, lat_b)))
This gives you: 这给您:
county_a county_b lat_a lon_a winner_a lat_b lon_b winner_b dist
1 1001 1001 32.53638 -86.64449 rep 32.53638 -86.64449 rep 0.00
2 1003 1001 30.65922 -87.74607 rep 32.53638 -86.64449 rep 233609.47
3 1005 1001 31.87067 -85.40546 rep 32.53638 -86.64449 rep 138247.91
4 1007 1001 33.01589 -87.12715 rep 32.53638 -86.64449 rep 69929.04
5 1009 1001 33.97745 -86.56725 rep 32.53638 -86.64449 rep 160579.78
6 1011 1001 32.10176 -85.71726 dem 32.53638 -86.64449 rep 99747.13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.