in R, I want to merge dhs survey data which includes the GPS location of the interviewee with temperature data air.mon.mean.v501.nc (Terrestrial Air Temperature: 1900-2017 Gridded Monthly Time Series) (https://psl.noaa.gov/data/gridded/data.UDel_AirT_Precip.html).
So far: My survey dataset (called "kids.dta") contains one observation per interviewee. Besides others, one variable is "lon", one is "lat", one is "interview_month", "interview_year". latitude values range from -90 to 90; longitude values range from -180 to 180;
e.g. lat 27.41974, lon 87.60922, interview_month 12, interview_year 2001
With the following R code, the temperature data follows the same coordinate grid as the survey data.
open_air_brick <- brick("air.mon.mean.v501.nc", var="air")
open_air <- rotate(open_air_brick)
Please note: lat and lon are rounded for the temperature data: e.g. lat 27.5, lon 87.5 (!)
What I want: I want to have a final dataset with the survey data and the corresponding monthly temperature of the year leading up to the month of survey (month of survey, and the 12 months before) for each observation based on their location (lon / lat).
What I guess I need to do:
find closest lon / lat in the temperature data for every observation in the survey data (e.g. lat 27.41974 is nearest to lat 27.5), and assign it to the survey data
merge survey data with temperature data based on prior assigned closest lon and lat, and ONLY extract temperature data for the month of survey and 12 months before the survey.
I have honestly no clue how to do it. So far, I have been working with the R Raster package.
If someone has any idea or hint, I will be so grateful!