Data interpolation in R

Suppose I have a 3 by 5 matrix in R:

4 5 5 6 8 3 4 4 5 6 2 3 3 3 4 

I would like to interpolate between these values ​​to create a 15 by 25 matrix. I would also like to indicate whether the interpolation is linear, Gaussian, etc. How can i do this?

For example, if I have a small matrix like this

 2 3 1 3 

and I want it to be 3 on 3, then it can look like

  2 2.5 3 1.5 2.2 3 1 2 3 
+6
source share
2 answers
 app <- function(x, n) approx(x, n=n)$y # Or whatever interpolation that you want apply(t(apply(x, 1, function(x) app(x, nc))), 2, function(x) app(x, nr)) [,1] [,2] [,3] [1,] 2.0 2.50 3 [2,] 1.5 2.25 3 [3,] 1.0 2.00 3 
+6
source

Once upon a time I wrote a similar toy, except that I was unable to determine the interpolation function. There is also raster::disaggregate .

 zexpand<-function(inarray, fact=2, interp=FALSE, ...) { # do same analysis of fact to allow one or two values, fact >=1 required, etc. fact<-as.integer(round(fact)) switch(as.character(length(fact)), '1' = xfact<-yfact<-fact, '2'= {xfact<-fact[1]; yfact<-fact[2]}, {xfact<-fact[1]; yfact<-fact[2];warning(' fact is too long. First two values used.')}) if (xfact < 1) { stop('fact[1] must be > 0') } if (yfact < 1) { stop('fact[2] must be > 0') } bigtmp <- matrix(rep(t(inarray), each=xfact), nrow(inarray), ncol(inarray)*xfact, byr=T) #does column expansion bigx <- t(matrix(rep((bigtmp),each=yfact),ncol(bigtmp),nrow(bigtmp)*yfact,byr=T)) # the interpolation would go here. Or use interp.loess on output (won't # handle complex data). Also, look at fields::Tps which probably does # a much better job anyway. Just do separately on Re and Im data return(invisible(bigx)) } 
0
source

All Articles