I have a data frame with columns id, latitude, longitude. I need to find near by meteorological stations and download data using RNOAA. The first step is to get station names with meteo_nearby_stations then download data with meteo_pull_monitors.
My question, is how do I retain the site id from df in the results from meteo_pull_monitors?
desired result can be seen here
library(rnoaa)
id<-c("07227500", "07308500", "07311700")
latitude<-c(35.47033,34.11009, 33.82064)
longitude<-c(101.87963,98.53172,-99.78648)
df<-data.frame(id,latitude,longitude)
met_test<-meteo_nearby_stations(df, lat_colname = "latitude",
lon_colname = "longitude", station_data = ghcnd_stations(),
var = c("TMAX","TMIN"), year_min = NULL, year_max = NULL,
radius = 200, limit = 3)
met_test_df<-do.call(rbind, lapply(met_test,as.data.frame))
met_id<-as.vector(met_test_df$id)
met_data<-meteo_pull_monitors(met_id, var = c("date","TMAX","TMIN"), date_min = "2020-01-01", date_max = "2020-06-01")
We can join the
site_iddata to the results of themeteo_nearby_stations()function by pulling the names of each element in themet_testlist.Fortunately, each element of
met_listcontains the name of thesite_idassociated with themeter_nearby_stations()request. We can access this information with thenames()function.To merge the site identifiers, we modify the
do.call()function from the original post to includelapply()with an anonymous function that assigns the correct name from the list to a column we namesite_id. Note that in order to loop through the list of data frames and access their names, we use a vector,1:length(met_test)to drive thelapply()function, and includemet_testas a second argument so we can use the index numberxto access both the correct list element and its name....and the output:
At this point we can extract the individual monitor data, and merge the
site_idnumbers by monitor id. First, we extract the monitor data.Then, we merge the site identifier data.
Finally, we print the first few rows of the result data frame.