0

For individual columns within a data frame, I want to replace NA values with imputed values corresponding to the closest existing values on either side. These imputed values should represent a linear line between the existing values - for example, 5, NA, NA, NA, 6 would become 5, 5.25, 5.50, 5.75. 6.

So, this data frame -

reprex_df <- data.frame(
  ID    = seq(from=1, to=13, by=1),
  Value = c(2,NA,NA,NA,3,NA,NA,NA,9,NA,NA,NA,11)
)
reprex_df
    ID Value
1    1     2
2    2    NA
3    3    NA
4    4    NA
5    5     3
6    6    NA
7    7    NA
8    8    NA
9    9     9
10  10    NA
11  11    NA
12  12    NA
13  13    11

would turn into this:

    ID  Value
1    1   2.00
2    2   2.25
3    3   2.50
4    4   2.75
5    5   3.00
6    6   4.50
7    7   6.00
8    8   7.50
9    9   9.00
10  10   9.50
11  11  10.00
12  12  10.50
13  13  11.00

Is there any simple (hopefully tidy) way to do this? FWIW, this is similar to this question, but not quite the same as that deals with a simple mean of the two existing values on either side of the NA.

jliebss
  • 33
  • 1
  • 5

0 Answers0