not very kind to give the data like that
you can use reprex to produce a nice output or dput(yourdata)!
here is a simple solution, since your grid is regular, I used the raster package to perform the interpolation
To take into account the altitude you have to dig a little bit more and find another method.
You can also perform more complex interpolation (based on fields package for example) using the function interpolate see the help of the function.
library(raster)
# data
df <- data.frame(
lon = c(39.9375, 39.9375, 40.0625, 40.0625),
lat = c(-105.3125, -105.4375, -105.3125, -105.4375),
pr = c(1200, 1000, 800, 900),
tem = c(8, 10, 5, 6.4),
z = c(2100, 2500, 2000, 2420)
)
# interpolate data on that pt
pt <- matrix(c(40.01, -105.3), ncol = 2)
# illustration
plot(ras)
points(pt, cex = 1.2, pch = 16)
# make a raster brick (regular grid, multiple variables)
ras <- rasterFromXYZ(df)
# perform interpolation (simple or bilinear)
extract(a, y = pt, method = "simple")
extract(a, y = pt, method = "bilinear")
I think you can manage to do it on multiple timesteps!
Good luck,
Cheers