[R] 2/3d interpolation from a regular grid to another regular grid

jiho jo.irisson at gmail.com
Wed Dec 5 23:45:38 CET 2007

On 2007-December-05  , at 16:47 , Scionforbai wrote:
>> I just read the description in ?Krig in the package fields which  
>> says:
>> " Fits a surface to irregularly spaced data. "
> Yes, that is the most general case. Regular data location is a subset
> of irregular. Anyway, kriging, just one g, after the name of Danie
> Krige, the south african statistician who first applied such method
> for minig survey.

ooops. sorry about the typo.

>> My problem is simpler
> ...
>> So it is really purely numerical.
> ...
>> I just hoped that R had that already coded ...
> Of course R has ... ;) If your grids are really as simple as the
> example you posted above, and you have a really little variability,
> all you need is a "moving average", the arithmetic mean of the two
> nearest points belonging to grid1 and grid2 respectively. I assume
> that your regularly shaped grids are values stored in matrix objects.
> The functions comes from the "diff.default" code (downloading the R
> source code, I assure, is worth):

I can imagine it is indeed. I use the source of packages functions  
very often.

> my.interp <- function(x, lag = 1)
> {
>     r <- unclass(x)  # don't want class-specific subset methods
>     i1 <- -1:-lag
>     r <- (r[i1] + r[-length(r):-(length(r)-lag+1)])/2
>     class(r) <- oldClass(x)
>     return(r)
> }
> Finally,
> g1 <- apply(grid1val,1,my.interp)
> g2 <- apply(grid2val,2,my.interp)
> give the interpolations on gridFinal, provided that all gridFinal
> points are within the grid1 and grid2 ones.
> If you want the mean from 4 points, you apply once more with lag=3,
> cbind/rbind to the result columns/rows o NAs, and you calculate the
> mean of the points of the two matrixes.
> This is the simplest (and quickest) moving average that you can do.
> For more complicated examples, and for 3d, you have to go a little
> further, but the principle holds.

Thanks very much. I'll test this soon (and it looks like the vector  
operation might even be directly translatable in Fortran which is  
nice since I'll need to do it in Fortran too).

Thanks again.


More information about the R-help mailing list