我有两个带邮政编码的data.frames。一个是房屋,另一个是气象站。我需要合并邮政编码附近的2个data.frames,因此我将气象站的温度数据与离它最近的房子合并。我不能合并的邮政编码的数量,因为它是不一样的2 datas.frames,因为一些房子没有气象站在相同的邮政编码。
这在R中是可能的吗?
非常感谢。
发布于 2019-09-25 03:07:51
这是基于这样的假设:气象站列表很小,因此不会影响计算时间:
library(dplyr)
library(purrr)
df_with_homes_stats = df_with_homes %>%
dplyr::mutate(closestZip = purrr::map_dbl(.x=homeZip,~df_with_stats$stationZip[which.min(abs(.x-df_with_stats$stationZip))])) %>%
dplyr::left_join(df_with_stats,by=c('closestZip'='stationZip'))这里,'df_with_homes‘是具有每个家庭的邮政编码的大型数据集,而'df_with_stats’对应于电台的邮政编码。这里的基本假设是邮政编码之间的距离与它们之间的数学差异成正比。
如果它起作用了,请告诉我。
https://stackoverflow.com/questions/58085588
复制相似问题