r - 如何加入位置数据(纬度,经度)

标签 r join difference fuzzyjoin

我必须创建数据集,一个包含某个位置(纬度、经度),这是测试,另一个包含纽约市所有邮政编码的纬度/经度信息,这是测试 2。

test <- structure(list(trip_count = 1:10, dropoff_longitude = c(-73.959862, 
                                                        -73.882202, -73.934113, -73.992203, -74.00563, -73.975189, -73.97448, 
                                                        -73.974838, -73.981377, -73.955093), dropoff_latitude = c(40.773617, 
                                                                                                                  40.744175, 40.715923, 40.749203, 40.726158, 40.729824, 40.763599, 
                                                                                                                  40.754135, 40.759987, 40.765224)), row.names = c(NA, -10L), class = c("tbl_df", 
                                                                                                                                                                                        "tbl", "data.frame"))
test2 <- structure(list(latitude = c(40.853017, 40.791586, 40.762174, 
40.706903, 40.825727, 40.739022, 40.750824, 40.673138, 40.815559, 
40.754591), longitude = c(-73.91214, -73.94575, -73.94917, -73.82973, 
-73.81752, -73.98205, -73.99289, -73.81443, -73.90771, -73.976238
), borough = c("Bronx", "Manhattan", "Manhattan", "Queens", "Bronx", 
"Manhattan", "Manhattan", "Queens", "Bronx", "Manhattan")), class = "data.frame", row.names = c(NA, 
-10L))

我现在正尝试加入这两个数据集,以便最终为每个 trip_count 得到一个 borough。到目前为止,我使用 difference_left_join 是这样的:

test %>% fuzzyjoin::difference_left_join(test2,by = c("dropoff_longitude" = "longitude" , "dropoff_latitude" = "latitude"), max_dist = 0.01)

尽管这种方法可行,但随着数据集变大,此连接会创建大量多重匹配项,因此我最终得到的数据集有时是初始测试 的十倍。有没有人有不同的方法来解决这个问题而不创建多堆匹配?或者有什么方法可以强制连接始终只对 test 中的每一行使用一个匹配项?我将不胜感激!

编辑:解决这个问题R dplyr left join - multiple returned values and new rows: how to ask for the first match only?也会解决我的问题。所以也许你们中的一个人对此有想法!

最佳答案

您可以使用 geo_join 函数并返回匹配项之间的距离,然后过滤到最接近的匹配项。

library(fuzzyjoin)
library(dplyr)

answer <-geo_left_join(test, test2, by = c("dropoff_longitude" = "longitude" , "dropoff_latitude" = "latitude"), 
                   max_dist = 2, distance_col = "dist") %>% 
         select(-"longitude", -"latitude")

answer  %>% group_by(trip_count) %>% slice_min(dist)

您可能需要向下调整“max_dist”的值以减少匹配数,这应该会提高性能但可能会生成过多的 NA。

更新
四舍五入到小数点后 3 位最多为 70 米/230 英尺偏移。四舍五入到更少的小数位数会减少唯一点的数量,但会增加最大偏移量。

以下是我将如何处理舍入下车位置和执行连接。它增加了复杂性,但可能有助于解决内存问题。我在这里没有考虑 group_by 函数,但它也可以。

#create a unique id for each rounded lon & lat
test$hash <-paste(round(test$dropoff_longitude, 3), round(test$dropoff_latitude, 3))
#the unique ids
uniques <- which(!duplicated(test$hash))
#create a reduced size data frame 
reduced <- data.frame(hash= test$hash, 
                      dropoff_longitude = round(test$dropoff_longitude, 3), 
                      dropoff_latitude = round(test$dropoff_latitude, 3))[uniques,]

#Preform matching here
#using the formula above or something else.
# adding the matched column onto the reduced dataframe
    reduced$matched <- letters[1:nrow(reduced)]
#this example is just adding on a column of letters

#merge back to the original adata set
test %>% left_join(reduced[ , c("hash", "matched")], by=("hash"))

关于r - 如何加入位置数据(纬度,经度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65160698/

相关文章:

sql - SQL 中的聚合和(连接)

Mysql Update with table joins - 用另一个表字段的总和更新一个表的字段

SQL - 连接其中一列是列表的表

python - 当一个单元格中存在多个值时,计算列之间的年份差异

r - 相当于 RStudio 构建面板按钮的开发工具

r - 更新下拉列表 Shiny

r - R中每月的累计金额

r - 取消嵌套或取消切割包含不同长度列表的数据帧

javascript - 比较 2 列表得到移动 id + 偏移量 + 方向

python - Pandas:计算数据框中所有行和特定行之间的差异