Reputation: 55
I have some multidimensional data structures that I need to do some interpolation on. I'm really finding it difficult to get an example of how to do this in R!
As an example if I have the array, old
:
old <- array(runif(10*12), dim=c(12,10))
str(old)
num [1:12, 1:10] 0.763 0.429 0.792 0.923 0.476 ...
what I would like to do is make the array new
:
new <- interp2d(old, newx=6, newy=5)
i.e. I want to change the dimensions of the array so that new is a 6*5 array of the old
data -- in this example it would be important to preserve the totals in the old
grid. The line above is an example of what I would like to do, I don't know how to and was hoping someone would??! Thanks!
Upvotes: 1
Views: 6074
Reputation: 226951
Here's a start, but not I'm sure it does what you want (preserve totals). In fact, I'm not sure I see how you can exactly preserve the totals when you're coarsening in this way ... unless you'd rather "bin" than "interpolate"?
library(sos)
findFn("{bilinear interpolation}")
set.seed(101)
old <- array(runif(10*12), dim=c(12,10))
library(fields)
interp2d <- function(old, newx, newy) {
interp.surface.grid(list(x=seq(nrow(old)),y=seq(ncol(old)),z=old),
list(x=seq(1,nrow(old),length=newx),
y=seq(1,ncol(old),length=newy)))$z
}
newmat <- interp2d(old, newx=6, newy=5)
Upvotes: 2