Sara
Sara

Reputation: 322

Interpolating three columns

I have a set of data in ranges like:

 x|y|z
-4|1|45
-4|2|68
-4|3|96
-2|1|56
-2|2|65
-2|3|89
 0|1|45
 0|2|56
 0|3|75
 2|1|23
 2|2|56
 2|3|75
 4|1|42
 4|2|65
 4|3|78

Here I need to interpolate between x and y using the z value.

I tried interpolating separately for x and y using z value by using the below code:

interpol<-approx(x,z,method="linear")
interpol_1<-approx(y,z,method="linear")

Now I'm trying to use all the three columns but values are coming wrong.

Upvotes: 0

Views: 138

Answers (1)

DJV
DJV

Reputation: 4863

In your script you forgot to direct to your data.frame. Note the use of $ in the approx function.

interpol <- approx(df$x,df$z,method="linear")
interpol_1 <- approx(df$y,df$z,method="linear")

Data:

df <- data.frame(
           x = c(-4, -4, -4, -2, -2, -2, 0, 0, 0, 2, 2, 2, 4, 4, 4),
           y = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3),
           z = c(45, 68, 96, 56, 65, 89, 45, 56, 75, 23, 56, 75, 42, 65, 78)
)

Upvotes: 1

Related Questions