Home > front end >  Linear interpolation in R for columns
Linear interpolation in R for columns

Time:07-06

I have a table with column called 'rates'. This column include missing values. I need to calculate linear function where missing values are calculated based on previous existing value and a value that follows the missing value, and there should be an equal interval between replaced missing values.

For example, column 'rates' include values: 0,66 Na Na Na 0,77 0,75 0,79 Na Na 0,79

And I need to get a new column where Na values will be replaced in R the way I described above.

CodePudding user response:

You could use na.approx from the zoo package. The function according to document:

Generic functions for replacing each NA with interpolated values.

Code:

df <- data.frame(rates = c(0.66, NA, NA, NA, 0.77, 0.75, 0.79, NA, NA, 0.79))

library(zoo)
df$new_rates <- na.approx(df$rates)
df
#>    rates new_rates
#> 1   0.66    0.6600
#> 2     NA    0.6875
#> 3     NA    0.7150
#> 4     NA    0.7425
#> 5   0.77    0.7700
#> 6   0.75    0.7500
#> 7   0.79    0.7900
#> 8     NA    0.7900
#> 9     NA    0.7900
#> 10  0.79    0.7900

Created on 2022-07-05 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related