Reputation: 586
I have two dataframes:
df1 <- data.frame(height = c(0, 100, 200, 300, 400), value = c(20, 30, 40, 50, 60))
df2 <- data.frame(height = c(40, 100, 200, 300, 500), value = c(NA, 35, 50, 60, 70))
df1 df2
height value height value
0 20 40 NA
100 30 100 35
200 40 200 50
300 50 300 60
400 60 500 70
Where I want to interpolate the values in df1 to fill in the missing height (40) in df2.
Upvotes: 1
Views: 158
Reputation: 39737
You can use approxfun
to Interpolate missing values of column based on values of another dataframe.
i <- is.na(df2$value)
df2$value[i] <- approxfun(df1$height, df1$value)(df2$height[i])
df2
# height value
#1 40 24
#2 100 35
#3 200 50
#4 300 60
#5 500 70
Upvotes: 1