在 R 中确定 'true' 地址位置的方法
Method to determine 'true' address location in R
我有一个很大的 AddressId 数据集需要清理。最终用户可以自己输入属于某个 AddressId 的坐标,因此某些 AddressId 具有许多(> 20)个略有不同(有时只有几米)的经度和纬度。例如:
AddressID Longitude Latitude
1234 77.037692 38.89864
1234 77.037872 38.88775
1234 77.048522 38.78553
现在,我想在 R 的帮助下确定哪个经度和哪个纬度最准确。取中位数或类似的东西是不可能的,因为坐标略有不同,而且几乎从来没有完全相同的坐标进入。
我的想法是,与另一个经度(分别为纬度)的距离最短的经度(分别为纬度)应该是最准确的。现在我想知道我可以使用哪种包和方法来回答这样的问题。
我尝试使用我在评论中链接的方法,只需要基本功能:
# Read in data.
df <- read.table(header = TRUE, text = "AddressID Longitude Latitude
1234 77.037692 38.89864
1234 77.037872 38.88775
1234 77.048522 38.78553")
# Get the radians.
df$LongitudeRadians <- df$Longitude * pi / 180
df$LatitudeRadians <- df$Latitude * pi / 180
# Get the cartesian coordinates.
df$x <- sin(df$LatitudeRadians) * cos(df$LongitudeRadians)
df$y <- sin(df$LatitudeRadians) * sin(df$LongitudeRadians)
df$z <- cos(df$LatitudeRadians)
# Get the means.
avgs <- aggregate.data.frame(df[c(1,6:8)], by = list(df$AddressID), FUN = "mean")
# Convert back to degrees.
avgs$LongitudeRadians <- atan2(avgs$y, avgs$x)
avgs$LatitudeRadians <- atan2(sqrt(avgs$x^2 + avgs$y^2), avgs$z)
avgs$Longitude <- avgs$LongitudeRadians * 180 / pi
avgs$Latitude <- avgs$LatitudeRadians * 180 / pi
avgs <- avgs[c(2,8:9)]
结果 分数 与仅计算经度和纬度的平均值不同,可能是因为您给定的 long/lat 靠得太近了。
但是,嘿,我今天学到了一些关于球坐标系的知识,所以即使这被否决了,我也很开心。
我有一个很大的 AddressId 数据集需要清理。最终用户可以自己输入属于某个 AddressId 的坐标,因此某些 AddressId 具有许多(> 20)个略有不同(有时只有几米)的经度和纬度。例如:
AddressID Longitude Latitude
1234 77.037692 38.89864
1234 77.037872 38.88775
1234 77.048522 38.78553
现在,我想在 R 的帮助下确定哪个经度和哪个纬度最准确。取中位数或类似的东西是不可能的,因为坐标略有不同,而且几乎从来没有完全相同的坐标进入。
我的想法是,与另一个经度(分别为纬度)的距离最短的经度(分别为纬度)应该是最准确的。现在我想知道我可以使用哪种包和方法来回答这样的问题。
我尝试使用我在评论中链接的方法,只需要基本功能:
# Read in data.
df <- read.table(header = TRUE, text = "AddressID Longitude Latitude
1234 77.037692 38.89864
1234 77.037872 38.88775
1234 77.048522 38.78553")
# Get the radians.
df$LongitudeRadians <- df$Longitude * pi / 180
df$LatitudeRadians <- df$Latitude * pi / 180
# Get the cartesian coordinates.
df$x <- sin(df$LatitudeRadians) * cos(df$LongitudeRadians)
df$y <- sin(df$LatitudeRadians) * sin(df$LongitudeRadians)
df$z <- cos(df$LatitudeRadians)
# Get the means.
avgs <- aggregate.data.frame(df[c(1,6:8)], by = list(df$AddressID), FUN = "mean")
# Convert back to degrees.
avgs$LongitudeRadians <- atan2(avgs$y, avgs$x)
avgs$LatitudeRadians <- atan2(sqrt(avgs$x^2 + avgs$y^2), avgs$z)
avgs$Longitude <- avgs$LongitudeRadians * 180 / pi
avgs$Latitude <- avgs$LatitudeRadians * 180 / pi
avgs <- avgs[c(2,8:9)]
结果 分数 与仅计算经度和纬度的平均值不同,可能是因为您给定的 long/lat 靠得太近了。
但是,嘿,我今天学到了一些关于球坐标系的知识,所以即使这被否决了,我也很开心。