3

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

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。有没有人有不同的方法来解决这个问题而不创建多个匹配?或者有什么办法可以强制连接总是只为每一行使用一个匹配项test?我将不胜感激!

编辑:解决这个问题R dplyr left join - 多个返回值和新行:如何只要求第一个匹配项?也会解决我的。所以也许你们中的一个人对此有一个想法!

4

1 回答 1

3

您可以使用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"))
于 2020-12-05T19:13:00.480 回答