0

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.

  • Welcome to SO! Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and your effort so far. However, based on your question it seems that you just want the mean between the previous and next values – Elia Jul 05 '22 at 10:27
  • Does this answer your question? [Interpolate NA values](https://stackoverflow.com/questions/7188807/interpolate-na-values) – harre Jul 05 '22 at 10:38

1 Answers1

0

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)

Quinten
  • 35,235
  • 5
  • 20
  • 53