# Bicubic/bilinear interpolation in R

I have a data set of x,y,z data and I'd like to do a bicubic interpolation. x and y are spatial coordinates and z is a temperature.

Below there are two images. The first one is a (gnuplot) plot of my dataset and the second one is an interpolated version (set pm3d interpolate 10,10).

Now I'd like to do this interpolation in R but I want a matrix of values as result and not a plot. The climates package seemed to provide this in R, but it does not work in R 2.13 anymore.

Is there another way/package to interpolate in R the way I want?  Check out the akima package's interp.

These functions implement bivariate interpolation onto a grid for irregularly spaced input data. Bilinear or bicubic spline interpolation is applied using different versions of algorithms from Akima.

Usage

interp(x, y, z, xo=seq(min(x), max(x), length = 40), yo=seq(min(y), max(y), length = 40), linear = TRUE, extrap=FALSE, duplicate = "error", dupfun = NULL, ncp = NULL)

I assume it will work if your data is regularly spaced as well.

• This worked fine within R. I still have problems on how to export the data in x,y,z notation. I'd appreciate if someone could comment on this Oct 7 '11 at 16:54
• I believe something like i <- interp (x, y, z) followed by c <- cbind (expand.grid (i$x, i$y), c(i$z)) and if you want nice names colnames (c) <- c("x", "y", "z") is close. I might have gotten the x and y reversed, so check that. Oct 7 '11 at 17:33 • the expand.grip function did the trick. I still don't quite understand the meaning of it but I will try to give it a read. Oct 10 '11 at 8:05 You could use image.smooth in the fields package. • Judging by the name, that's probably doing smoothing, not interpolation - the difference being whether the original points are preserved exactly, or not. Dec 17 '12 at 16:49 To export the data you have interpolated , you could use the follow way: model <- interp(x, y, z, xo=seq(min(x), max(x), length = 40), yo=seq(min(y), max(y), length = 40), linear = TRUE, extrap=FALSE, duplicate = "error", dupfun = NULL, ncp = NULL) interpData <- model$z


library(rJava)

write.xlsx (interpData, "c:/multivariateInterp.xlsx")