Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolating data in R

Let's 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 in between these values to create a matrix of size 15 by 25. I would also like to specify if 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 become 3 by 3, then it might look like

  2    2.5   3
  1.5  2.2   3
  1    2     3 
like image 387
CodeGuy Avatar asked Apr 15 '13 21:04

CodeGuy


People also ask

How do you interpolate in R?

method:specifies the interpolation method to be used. Choices are “linear” or “constant”. n:If xout is not specified, interpolation takes place at n equally spaced points spanning the interval [min(x), max(x)]. yleft: the value to be returned when input x values are less than min(x).

What does approx do in R?

approx returns a list with components x and y , containing n coordinates which interpolate the given data points according to the method (and rule ) desired. The function approxfun returns a function performing (linear or constant) interpolation of the given data points.

What does interpolate mean in data?

Interpolation means determining a value from the existing values in a given data set. Another way of describing it is the act of inserting or interjecting an intermediate value between two other values.


1 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
like image 136
Matthew Lundberg Avatar answered Nov 15 '22 05:11

Matthew Lundberg