0

我正在寻找一种有效的方法将大型数据集中的每条记录链接到其最近的 NOAA 气象站。数据集包含 9 位邮政编码,NOAA 气象站有经纬度信息。任何人都有最好的方法来做到这一点?谢谢!

编辑:使用有效的代码进行更新,以防其他人正在寻找最近的 NOAA 气象站到一组邮政编码/如果有更好的方法来做到这一点的建议。

基于此问题中提供的代码:查找最近的邻居(log,lat),然后是下一个最近的邻居,依此类推,用于 R 中两个数据集之间的所有点

temp_stations 从https://www1.ncdc.noaa.gov/pub/data/normals/1981-2010/station-inventories/temp-inventory.txt下载(用于开发温度数据集的气象站)

zipcodes 是一个包,其中包含美国每个邮政编码的 lat long 数据集。

install.packages("zipcode")
require(zipcode)
data(zipcode)
#prime.zips is a subset of "zipcode" created by selecting just the zip codes contained in my original dataset. running the code below on the whole zipcode dataset crashed R on my computer.
install.packages("geosphere")
require(geosphere)
mat <- distm(prime.zips[ ,c('longitude','latitude')], temp_stations[ ,c(3,2)], fun=distGeo)
# assign the weather station id to each record in prime.zips based on shortest distance in the matrix
prime.zips$nearest.station <- temp_stations$station.id[apply(mat, 1, which.min)]
4

0 回答 0