Shekhar Sahu
Shekhar Sahu

Reputation: 573

Interpolation function approx() in r gives error - need at least two non-NA values to interpolate

I am using R Studio on Windows 8 machine. I am trying to interpolate a point between two points.

x1 = -159.9, y1 = 56.5, 
x2 = -159.9, y2 = 56.3

I am using approx() function in the following manner (reproducible)

approx(c(-159.9,-159.9), c(56.5,56.3), n = 3)

which gives me an error

Error in approx(c(-159.9, -159.9), c(56.5, 56.3), n = 3) : 
need at least two non-NA values to interpolate

Its expecting two non-NA values, which I have provided.

The function is working flawlessly for other points. Just this is the problem. If you have come across any such error, please let me know how did you solve this?

Upvotes: 1

Views: 14416

Answers (2)

robbertjan94
robbertjan94

Reputation: 148

The approx function can't interpolate values where the x-coordinates are the same.

Hence, I would tackle this problem as follows:

  1. Group all the cases where the x-coordinates are equal and aggregate them by for example the median, mean, or a custom built function
  2. Use you intended interpolation scheme, for example the approx function.

Upvotes: 3

AK88
AK88

Reputation: 3026

From the Details of ?approx():

The inputs can contain missing values which are deleted, so at least two complete (x, y) pairs are required (for method = "linear", one otherwise). If there are duplicated (tied) x values and ties is a function it is applied to the y values for each distinct x value.

Upvotes: 3

Related Questions