I have daily meanvalues of temperature data at different locations (X1, X2, ...) and I would like to interpolate maps with them. I created a long format data objects by loading them from formatted excel sheets such as:
library(reshape2)
tempdata <- read.csv("...", sep=";")
names(tempdata) <- c("date", paste("X", 1:73))
head(tempdata)
# date X1 X2 X3 X4 X5 X6 X7
# 1 1 7.3 6.6 6.7 5.8 6.1 6.1 5.5
# 2 2 7.5 6.6 6.6 5.6 4.8 4.7 3.9
# 3 3 8.8 7.7 7.6 7.0 7.0 6.0 5.8
# 4 4 8.5 7.4 7.5 7.0 7.3 5.9 5.5
# 5 5 7.7 6.7 6.9 6.1 6.8 5.1 4.1
# 6 6 7.5 6.7 6.8 6.0 6.4 5.0 4.1
same for the latitude longitude of the locations X1, X2, ...:
lat.lon <- read.csv("...", sep=";")
rownames(lat.lon) <- c(paste0("X",1:73))
head(lat.lon)
# latitude longitude
# X1 54.1650 6.3458
# X2 54.1667 7.4500
# X3 54.1832 7.8856
# X4 55.0114 8.4158
# X5 54.5068 9.5393
# X6 54.5214 11.0522
I merged them into long-format:
res <- merge(
melt(tempdata, id.vars="date"),
lat.lon,
by.x="variable", by.y="row.names"
)
head(res)
# variable date value latitude longitude
# X1 1 9.9 54.165 6.3458
# X1 2 8.9 54.165 6.3458
# X1 3 7.8 54.165 6.3458
# X1 4 9.2 54.165 6.3458
# X1 5 8.7 54.165 6.3458
# X1 6 8.4 54.165 6.3458
with
coordinates(res) = ~longitude+latitude
I can use spplot to plot them at the correct locations, also with country boundaries:
library(maptools)
load(url('http://gadm.org/data/rda/DEU_adm0.RData'))
GE <- gadm
GE <- spChFIDs(GE, paste("GE", rownames(GE), sep = "_"))
spplot(res["value"], sp.layout = list("sp.polygons", GE), col.regions=bpy.colors(20))
I would like to use IDW for single days of the observations, but the idw methods from the packages Ive found (e.g. gstat) need other 'gridded' data objects. How can i create such data objects in order to interpolate them with such methods?