0

I'm looking for an efficient way to link each record in a large dataset to its nearest NOAA weather station. The dataset contains 9-digit zip codes, and NOAA weather stations have lat long info. Anyone have tips on the best way to do this? Thanks!

EDIT: updating with code that worked in case anyone else is looking to find nearest NOAA weather station to a set of zip codes/ if there are suggestions for better ways to do this.

code based on that provided in this question: Finding nearest neighbour (log, lat), then the next closest neighbor, and so on for all points between two datasets in R

temp_stations is downloaded from https://www1.ncdc.noaa.gov/pub/data/normals/1981-2010/station-inventories/temp-inventory.txt (weather stations used in development of temperature dataset)

zipcodes is a package that contains a dataset with lat long for each zip code in the US.

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)]
Swatso
  • 1
  • 2
  • 2
    Welcome to SO. For a better response to your question, you should create a [minimum reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to detail your problem and your attempts to solve it so far. – Kevin Arseneau Nov 10 '17 at 02:32
  • What research have you done so fat. A quick Google search revealed [this Stack Overflow question](https://stackoverflow.com/questions/11354490/latitude-and-longitude-based-on-zip-code) which shows how to get the lat long of a zip code. From there it is a simple [Haversine problem](https://rosettacode.org/wiki/Haversine_formula). – Tom Aranda Nov 10 '17 at 02:40
  • Thanks, I'm new to the site so will work on asking better questions. Just found the rnoaa package, which contains many functions for working with weather station data, so I'm hoping that will provide some clues. – Swatso Nov 10 '17 at 02:58

0 Answers0