r语言 - 计算数据框中两个经度和经度之间的距离



我有一个数据集,其中包括每个单独属性地址的纬度和经度。此外,我还创建了两个新列(icelat、icelog(,其中包括犹他州一座特定建筑的纬度和经度。

数据如下:

RowID PropertyAddressLatitude PropertyAddressLongitude  icelat    icelog
1: 000D655E-1AEA-E811-80C3-3863BB430E3F                38.65195                -109.4085 40.2351 -111.6384
2: 000F655E-1AEA-E811-80C3-3863BB430E3F                38.50952                -109.4763 40.2351 -111.6384
3: 0012CB31-D004-E911-80C7-3863BB43E813                      NA                       NA 40.2351 -111.6384
4: 0013655E-1AEA-E811-80C3-3863BB430E3F                38.54184                -109.5031 40.2351 -111.6384
5: 0014655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384
6: 0015655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384

我想创建一个新的列,称为"距离",即从每个房产的纬度和经度到犹他州特定建筑的距离,以英里为单位。

我尝试了几种不同的方法来使用Geosphere包,但无法让它运行所有的"PropertyAddressLatitude"one_answers"PropertyAddressLongitude"观测,并自动对"icelat"one_answers"icelog"进行计算

默认单位为米,因此我将就地转换。

meter2mile <- 0.000621371
dat[, distance := meter2mile * geosphere::distVincentyEllipsoid(
cbind(PropertyAddressLongitude, PropertyAddressLatitude),
cbind(icelog, icelat)) ]
dat
#                                   RowID PropertyAddressLatitude PropertyAddressLongitude  icelat    icelog distance
# 1: 000D655E-1AEA-E811-80C3-3863BB430E3F                38.65195                -109.4085 40.2351 -111.6384 161.7148
# 2: 000F655E-1AEA-E811-80C3-3863BB430E3F                38.50952                -109.4763 40.2351 -111.6384 166.0397
# 3: 0012CB31-D004-E911-80C7-3863BB43E813                      NA                       NA 40.2351 -111.6384       NA
# 4: 0013655E-1AEA-E811-80C3-3863BB430E3F                38.54184                -109.5031 40.2351 -111.6384 163.4240
# 5: 0014655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384       NA
# 6: 0015655E-1AEA-E811-80C3-3863BB430E3F                      NA                       NA 40.2351 -111.6384       NA

数据

dat <- as.data.table(structure(list(RowID = c("000D655E-1AEA-E811-80C3-3863BB430E3F", "000F655E-1AEA-E811-80C3-3863BB430E3F", "0012CB31-D004-E911-80C7-3863BB43E813", "0013655E-1AEA-E811-80C3-3863BB430E3F", "0014655E-1AEA-E811-80C3-3863BB430E3F", "0015655E-1AEA-E811-80C3-3863BB430E3F"), PropertyAddressLatitude = c(38.65195, 38.50952, NA, 38.54184, NA, NA), PropertyAddressLongitude = c(-109.4085, -109.4763, NA, -109.5031, NA, NA), icelat = c(40.2351, 40.2351, 40.2351, 40.2351, 40.2351, 40.2351), icelog = c(-111.6384, -111.6384, -111.6384, -111.6384, -111.6384, -111.6384)), row.names = c(NA, -6L), class = c("data.table", "data.frame")))

(我从你的样本数据推断出data.table,如果这是不正确的,请告知。(

相关内容

  • 没有找到相关文章

最新更新